2017-12-13 11:01:37 +00:00
|
|
|
/* @flow */
|
|
|
|
|
2018-12-03 17:01:09 +00:00
|
|
|
import BigNumber from 'bignumber.js';
|
|
|
|
|
2018-12-28 15:16:24 +00:00
|
|
|
export const toDecimalAmount = (amount: string | number, decimals: number): string => {
|
|
|
|
try {
|
2019-02-19 18:10:47 +00:00
|
|
|
const bAmount = new BigNumber(amount);
|
|
|
|
// BigNumber() returns NaN on non-numeric string
|
|
|
|
if (bAmount.isNaN()) {
|
|
|
|
throw new Error('Amount is not a number');
|
|
|
|
}
|
|
|
|
return bAmount.div(10 ** decimals).toString(10);
|
2018-12-28 15:16:24 +00:00
|
|
|
} catch (error) {
|
|
|
|
return '0';
|
|
|
|
}
|
|
|
|
};
|
2018-12-03 17:01:09 +00:00
|
|
|
|
2018-12-28 15:16:24 +00:00
|
|
|
export const fromDecimalAmount = (amount: string | number, decimals: number): string => {
|
|
|
|
try {
|
2019-02-19 18:10:47 +00:00
|
|
|
const bAmount = new BigNumber(amount);
|
|
|
|
// BigNumber() returns NaN on non-numeric string
|
|
|
|
if (bAmount.isNaN()) {
|
|
|
|
throw new Error('Amount is not a number');
|
|
|
|
}
|
|
|
|
return bAmount.times(10 ** decimals).toString(10);
|
2018-12-28 15:16:24 +00:00
|
|
|
} catch (error) {
|
|
|
|
return '0';
|
|
|
|
}
|
|
|
|
};
|