Change decimals, move unit (#695)

* Change decimals, move unit

* CHANGELOG
This commit is contained in:
Jaco Greeff
2020-09-27 09:49:47 +02:00
committed by GitHub
parent eb7270ff3f
commit 81acc81ef7
3 changed files with 28 additions and 27 deletions
+25 -25
View File
@@ -17,25 +17,25 @@ describe('formatBalance', (): void => {
it('formats 123,456,789,000 (decimals=15)', (): void => {
expect(
formatBalance(TESTVAL, { decimals: 15, withSi: true })
).toEqual('123.456µ Unit');
).toEqual('123.4567 µUnit');
});
it('formats BigInt numbers', (): void => {
expect(
formatBalance(123456789000n, { decimals: 15, withSi: true })
).toEqual('123.456µ Unit');
).toEqual('123.4567 µUnit');
});
it('formats 123,456,789,000 (decimals=15, old style)', (): void => {
expect(
formatBalance(TESTVAL, { withSi: true }, 15)
).toEqual('123.456µ Unit');
).toEqual('123.4567 µUnit');
});
it('formats 123,456,789,000 (decimals=36, option)', (): void => {
expect(
formatBalance(TESTVAL, { decimals: 36, withSi: true })
).toEqual('0.123y Unit');
).toEqual('0.1234 yUnit');
});
it('formats 123,456,789,000 (decimals=15, Compact)', (): void => {
@@ -47,91 +47,91 @@ describe('formatBalance', (): void => {
expect(
formatBalance(compact, { decimals: 15, withSi: true })
).toEqual('123.456µ Unit');
).toEqual('123.4567 µUnit');
});
it('formats 123,456,789,000 (decimals=12)', (): void => {
expect(
formatBalance(TESTVAL, { decimals: 12, withSi: true })
).toEqual('123.456m Unit');
).toEqual('123.4567 mUnit');
});
it('formats 123,456,789,000 (decimals=9)', (): void => {
expect(
formatBalance(TESTVAL, { withSi: true }, 9)
).toEqual('123.456 Unit');
).toEqual('123.4567 Unit');
});
it('formats 123,456,789,000 (decimals=9, no unit)', (): void => {
expect(
formatBalance(TESTVAL, { withSi: true, withUnit: false }, 9)
).toEqual('123.456');
).toEqual('123.4567');
});
it('formats 123,456,789,000 (decimals=9, unit given)', (): void => {
expect(
formatBalance(TESTVAL, { withSi: true, withUnit: 'FOO' }, 9)
).toEqual('123.456 FOO');
).toEqual('123.4567 FOO');
});
it('formats 123,456,789,000 (decimals=12, no SI)', (): void => {
expect(
formatBalance(TESTVAL, { withSi: false }, 12)
).toEqual('123.456');
).toEqual('123.4567');
});
it('formats 123,456,789,000 (decimals=12, full SI)', (): void => {
expect(
formatBalance(TESTVAL, { withSiFull: true }, 12)
).toEqual('123.456 milli Unit');
).toEqual('123.4567 milli Unit');
});
it('formats 123,456,789,000 (decimals=12, full SI, no unit)', (): void => {
expect(
formatBalance(TESTVAL, { withSiFull: true, withUnit: false }, 12)
).toEqual('123.456 milli');
).toEqual('123.4567 milli');
});
it('formats 123,456,789,000 (decimals=9, full SI)', (): void => {
expect(
formatBalance(TESTVAL, { withSiFull: true }, 9)
).toEqual('123.456 Unit');
).toEqual('123.4567 Unit');
});
it('formats 123,456,789,000 (decimals=6)', (): void => {
expect(
formatBalance(TESTVAL, { withSi: true }, 6)
).toEqual('123.456k Unit');
).toEqual('123.4567 kUnit');
});
it('formats 123,456,789,000 (decimals=6, unit specified)', (): void => {
expect(
formatBalance(TESTVAL, { withSi: true, withUnit: 'BAR' }, 6)
).toEqual('123.456k BAR');
).toEqual('123.4567 kBAR');
});
it('formats 123,456,789,000 * 10 (decimals=12)', (): void => {
expect(
formatBalance(TESTVAL.muln(10), { withSi: true }, 12)
).toEqual('1.234 Unit');
).toEqual('1.2345 Unit');
});
it('formats 123,456,789,000 * 100 (decimals=12)', (): void => {
expect(
formatBalance(TESTVAL.muln(100), { withSi: true }, 12)
).toEqual('12.345 Unit');
).toEqual('12.3456 Unit');
});
it('formats 123,456,789,000 * 1000 (decimals=12)', (): void => {
expect(
formatBalance(TESTVAL.muln(1000), { withSi: true }, 12)
).toEqual('123.456 Unit');
).toEqual('123.4567 Unit');
});
it('formats -123,456,789,000 (decimals=15)', (): void => {
expect(
formatBalance(new BN('-123456789000'), { withSi: true }, 15)
).toEqual('-123.456µ Unit');
).toEqual('-123.4567 µUnit');
});
});
@@ -139,25 +139,25 @@ describe('formatBalance', (): void => {
it('formats 123,456,789,000 (decimals=12, forceUnit=base)', (): void => {
expect(
formatBalance(TESTVAL, { forceUnit: '-' }, 12)
).toEqual('0.123 Unit');
).toEqual('0.1234 Unit');
});
it('formats 123,456,789,000 (decimals=9, forceUnit=base)', (): void => {
expect(
formatBalance(TESTVAL, { forceUnit: '-' }, 9)
).toEqual('123.456 Unit');
).toEqual('123.4567 Unit');
});
it('formats 123,456,789,000 (decimals=7, forceUnit=base)', (): void => {
expect(
formatBalance(TESTVAL, { forceUnit: '-' }, 7)
).toEqual('12,345.678 Unit');
).toEqual('12,345.6789 Unit');
});
it('formats 123,456,789,000 (decimals=15, forceUnit=µ)', (): void => {
expect(
formatBalance(TESTVAL, { forceUnit: 'µ' }, 15)
).toEqual('123.456µ Unit');
).toEqual('123.4567 µUnit');
});
});
@@ -196,7 +196,7 @@ describe('formatBalance', (): void => {
expect(
formatBalance(TESTVAL)
).toEqual('123.456m Unit');
).toEqual('123.4567 mUnit');
});
it('formats 123,456,789,000 (defaultUnit=TEST)', (): void => {
@@ -204,7 +204,7 @@ describe('formatBalance', (): void => {
expect(
formatBalance(TESTVAL)
).toEqual('123.456m TEST');
).toEqual('123.4567 mTEST');
});
});
+2 -2
View File
@@ -67,13 +67,13 @@ function _formatBalance <ExtToBn extends ToBn> (input?: number | string | BN | B
const mid = text.length - (decimals + si.power);
const prefix = text.substr(0, mid);
const padding = mid < 0 ? 0 - mid : 0;
const postfix = `${`${new Array(padding + 1).join('0')}${text}`.substr(mid < 0 ? 0 : mid)}000`.substr(0, 3);
const postfix = `${`${new Array(padding + 1).join('0')}${text}`.substr(mid < 0 ? 0 : mid)}0000`.substr(0, 4);
const units = withSi || withSiFull
? si.value === '-'
? withUnit
? ` ${isBoolean(withUnit) ? si.text : withUnit}`
: ''
: `${withSiFull ? ` ${si.text}` : si.value}${withUnit ? ` ${isBoolean(withUnit) ? SI[SI_MID].text : withUnit}` : ''}`
: ` ${withSiFull ? si.text : si.value}${withUnit ? `${withSiFull ? ' ' : ''}${(isBoolean(withUnit) ? SI[SI_MID].text : withUnit)}` : ''}`
: '';
return `${isNegative ? '-' : ''}${formatDecimal(prefix || '0')}.${postfix}${units}`;