Skip to content

Commit

Permalink
fromTokenMinimalUnit
Browse files Browse the repository at this point in the history
  • Loading branch information
estebanmino committed Oct 16, 2020
1 parent 70be92a commit 39287f6
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 1 deletion.
3 changes: 2 additions & 1 deletion app/util/number.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/**
* Collection of utility functions for consistent formatting and conversion
*/
import { BN } from 'ethereumjs-util';
import { addHexPrefix, BN } from 'ethereumjs-util';
import convert from 'ethjs-unit';
import { util } from '@metamask/controllers';
import numberToBN from 'number-to-bn';
Expand Down Expand Up @@ -36,6 +36,7 @@ export function fromWei(value = 0, unit = 'ether') {
* @returns {string} - String containing the new number
*/
export function fromTokenMinimalUnit(minimalInput, decimals) {
minimalInput = addHexPrefix(Number(minimalInput).toString(16));
let minimal = numberToBN(minimalInput);
const negative = minimal.lt(new BN(0));
const base = toBN(Math.pow(10, decimals).toString());
Expand Down
7 changes: 7 additions & 0 deletions app/util/number.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,13 @@ describe('Number utils :: fromTokenMinimalUnit', () => {
expect(fromTokenMinimalUnit(new BN('1337'), 0)).toEqual('1337');
expect(fromTokenMinimalUnit(new BN('1337'), 18)).toEqual('0.000000000000001337');
});

it('fromTokenMinimalUnit using exp number', () => {
expect(fromTokenMinimalUnit(1e22, 6)).toEqual('10000000000000000');
expect(fromTokenMinimalUnit(1e2, 6)).toEqual('0.0001');
expect(fromTokenMinimalUnit(1e16, 6)).toEqual('10000000000');
expect(fromTokenMinimalUnit(1e18, 18)).toEqual('1');
});
});

describe('Number utils :: toTokenMinimalUnit', () => {
Expand Down

0 comments on commit 39287f6

Please sign in to comment.