Handing high precision scenes (#450)

This commit is contained in:
WoeOm
2019-07-30 07:21:23 +02:00
committed by Jaco Greeff
parent ef1993a101
commit de7e9f8fba
3 changed files with 10 additions and 2 deletions
@@ -20,6 +20,12 @@ describe('formatBalance', (): void => {
).toEqual('123.456µ Unit');
});
it('formats 123,456,789,000 (decimals=36)', (): void => {
expect(
formatBalance(TESTVAL, true, 36)
).toEqual('0.123y Unit');
});
it('formats 123,456,789,000 (decimals=15, Compact)', (): void => {
const compact = {
toBn: (): BN => TESTVAL,
+2 -1
View File
@@ -53,7 +53,8 @@ function _formatBalance <ExtToBn extends ToBn> (input?: number | string | BN | E
const si = calcSi(text, decimals);
const mid = text.length - (decimals + si.power);
const prefix = text.substr(0, mid);
const postfix = `${text.substr(mid)}000`.substr(0, 3);
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 units = withSi
? (
si.value === '-'
+2 -1
View File
@@ -31,7 +31,8 @@ export const SI: SiDef[] = [
];
export function calcSi (text: string, decimals: number): SiDef {
return SI[(SI_MID - 1) + Math.ceil((text.length - decimals) / 3)] || SI[SI.length - 1];
const siDefIndex = (SI_MID - 1) + Math.ceil((text.length - decimals) / 3);
return SI[siDefIndex] || SI[siDefIndex < 0 ? 0 : SI.length - 1];
}
// Given a SI type (e.g. k, m, Y) find the SI definition