Perbill/Permill toHuman as percentage (#2307)

* Perbill/Permill toHuman as percentage

* Expectation to percentage
This commit is contained in:
Jaco Greeff
2020-05-26 13:51:05 +02:00
committed by GitHub
parent 9a14c65336
commit 4b1a9ab9e1
3 changed files with 15 additions and 2 deletions
+13 -1
View File
@@ -15,6 +15,14 @@ export type UIntBitLength = 8 | 16 | 32 | 64 | 128 | 256;
export const DEFAULT_UINT_BITS = 64;
const PER_B = new BN(1_000_000_000);
const PER_M = new BN(1_000_000);
const MUL_P = new BN(1_00_00);
function toPercentage (value: BN, divisor: BN): string {
return `${(value.mul(MUL_P).div(divisor).toNumber() / 100).toFixed(2)}%`;
}
/** @internal */
function decodeAbstracIntU8a (value: Uint8Array, bitLength: UIntBitLength, isNegative: boolean): string {
if (!value.length) {
@@ -159,7 +167,11 @@ export default abstract class AbstractInt extends BN implements Codec {
? this.isMax()
? 'everything'
: formatBalance(this, { decimals: this.registry.chainDecimals, withSi: true, withUnit: this.registry.chainToken })
: formatNumber(this);
: this instanceof this.registry.createClass('Perbill')
? toPercentage(this, PER_B)
: this instanceof this.registry.createClass('Permill')
? toPercentage(this, PER_M)
: formatNumber(this);
}
/**
+1 -1
View File
@@ -10,7 +10,7 @@ const registry = new TypeRegistry();
describe('Linkage', (): void => {
it('decodes with ValidatorPrefs', (): void => {
const LINKA = { next: '5GznmRvdi5htUJKnMSWJgJUzSJJXSvWuHRSEdyUbHJZDNcwU', previous: null };
const PREFS = { commission: '100,000,000' };
const PREFS = { commission: '10.00%' };
// prefs sanity check
expect(