diff --git a/package.json b/package.json index e15ed67..9fe6d54 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "@wharfkit/antelope", "description": "Library for working with Antelope powered blockchains.", - "version": "1.0.7", + "version": "1.0.8-rc2", "homepage": "https://github.com/wharfkit/antelope", "license": "BSD-3-Clause-No-Military-License", "main": "lib/antelope.js", diff --git a/src/chain/asset.ts b/src/chain/asset.ts index 30d88fe..dc6a8f2 100644 --- a/src/chain/asset.ts +++ b/src/chain/asset.ts @@ -62,6 +62,26 @@ export class Asset implements ABISerializableObject { return new this(Int64.from(0), Asset.Symbol.abiDefault()) } + static formatUnits(units: Int64Type, precision: number) { + const digits = Int64.from(units).toString().split('') + let negative = false + if (digits[0] === '-') { + negative = true + digits.shift() + } + while (digits.length <= precision) { + digits.unshift('0') + } + if (precision > 0) { + digits.splice(digits.length - precision, 0, '.') + } + let rv = digits.join('') + if (negative) { + rv = '-' + rv + } + return rv + } + constructor(units: Int64, symbol: Asset.Symbol) { this.units = units this.symbol = symbol @@ -80,30 +100,17 @@ export class Asset implements ABISerializableObject { this.units = this.symbol.convertFloat(newValue) } + get quantity(): string { + return Asset.formatUnits(this.units, this.symbol.precision) + } + toABI(encoder: ABIEncoder) { this.units.toABI(encoder) this.symbol.toABI(encoder) } toString() { - const digits = this.units.toString().split('') - let negative = false - if (digits[0] === '-') { - negative = true - digits.shift() - } - const p = this.symbol.precision - while (digits.length <= p) { - digits.unshift('0') - } - if (p > 0) { - digits.splice(digits.length - p, 0, '.') - } - let rv = digits.join('') - if (negative) { - rv = '-' + rv - } - return rv + ' ' + this.symbol.name + return this.quantity + ' ' + this.symbol.name } toJSON() {