Skip to content

Commit 09d11c4

Browse files
committed
Adds toPrecisionWithoutTrailingZeros utility
1 parent 5c74420 commit 09d11c4

File tree

2 files changed

+60
-0
lines changed

2 files changed

+60
-0
lines changed

ui/app/helpers/utils/util.js

+16
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import punycode from 'punycode'
22
import abi from 'human-standard-token-abi'
3+
import BigNumber from 'bignumber.js'
34
import ethUtil from 'ethereumjs-util'
45
import { DateTime } from 'luxon'
56

@@ -355,3 +356,18 @@ export function checkExistingAddresses (address, list = []) {
355356

356357
return list.some(matchesAddress)
357358
}
359+
360+
/**
361+
* Given a number and specified precision, returns that number in base 10 with a maximum of precision
362+
* significant digits, but without any trailing zeros after the decimal point To be used when wishing
363+
* to display only as much digits to the user as necessary
364+
*
365+
* @param {string | number | BigNumber} n - The number to format
366+
* @param {number} precision - The maximum number of significant digits in the return value
367+
* @returns {string} The number in decimal form, with <= precision significant digits and no decimal trailing zeros
368+
*/
369+
export function toPrecisionWithoutTrailingZeros (n, precision) {
370+
return (new BigNumber(n))
371+
.toPrecision(precision)
372+
.replace(/(\.[0-9]*[1-9])0*|(\.0*)/u, '$1')
373+
}

ui/app/helpers/utils/util.test.js

+44
Original file line numberDiff line numberDiff line change
@@ -348,4 +348,48 @@ describe('util', function () {
348348
assert(util.checkExistingAddresses('b', tokenList) === false)
349349
})
350350
})
351+
352+
describe('toPrecisionWithoutTrailingZeros', function () {
353+
const testData = [
354+
{ args: ['0', 9], result: '0' },
355+
{ args: [0, 9], result: '0' },
356+
{ args: ['0.0', 9], result: '0' },
357+
{ args: ['0.000000000000', 9], result: '0' },
358+
{ args: ['1', 9], result: '1' },
359+
{ args: [1 ], result: '1' },
360+
{ args: ['1.0', 9], result: '1' },
361+
{ args: ['1.000000000', 9], result: '1' },
362+
{ args: ['000000001', 9], result: '1' },
363+
{ args: ['000000001.0', 9], result: '1' },
364+
{ args: ['100000000', 9], result: '100000000' },
365+
{ args: ['100000000.00001', 9], result: '100000000' },
366+
{ args: ['100.00001', 9], result: '100.00001' },
367+
{ args: ['100.00001000', 9], result: '100.00001' },
368+
{ args: ['100.000010001', 9], result: '100.00001' },
369+
{ args: ['10.010101', 9], result: '10.010101' },
370+
{ args: ['0.1', 5], result: '0.1' },
371+
{ args: ['0.10', 5], result: '0.1' },
372+
{ args: ['0.1010', 5], result: '0.101' },
373+
{ args: ['0.01001', 5], result: '0.01001' },
374+
{ args: ['0.010010', 5], result: '0.01001' },
375+
{ args: ['0.010011', 5], result: '0.010011' },
376+
{ args: ['1.01005', 5], result: '1.0101' },
377+
{ args: ['1.000049', 5], result: '1' },
378+
{ args: ['1.00005', 5], result: '1.0001' },
379+
{ args: ['0.0000123456789', 9], result: '0.0000123456789' },
380+
{ args: ['1.0000123456789', 10], result: '1.000012346' },
381+
{ args: ['10000.0000012345679', 10], result: '10000' },
382+
{ args: ['1000000000000', 10], result: '1e+12' },
383+
{ args: ['1000050000000', 10], result: '1.00005e+12' },
384+
{ args: ['100000000000000000000', 10], result: '1e+20' },
385+
{ args: ['100005000000000000000', 10], result: '1.00005e+20' },
386+
{ args: ['100005000000000000000.0', 10], result: '1.00005e+20' },
387+
]
388+
389+
testData.forEach(({ args, result }) => {
390+
it(`should return ${result} when passed number ${args[0]} and precision ${args[1]}`, function () {
391+
assert.equal(util.toPrecisionWithoutTrailingZeros(...args), result)
392+
})
393+
})
394+
})
351395
})

0 commit comments

Comments
 (0)