You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
trezor-wallet/src/utils/formatUtils.js

72 lines
1.9 KiB

7 years ago
/* @flow */
import BigNumber from 'bignumber.js';
const currencyUnitsConstant: string = 'mbtc2';
7 years ago
export const formatAmount = (n: number, coinInfo: any, currencyUnits: string = currencyUnitsConstant): string => {
const amount = (n / 1e8);
7 years ago
if (coinInfo.isBitcoin && currencyUnits === 'mbtc' && amount <= 0.1 && n !== 0) {
const s = (n / 1e5).toString();
7 years ago
return `${s} mBTC`;
}
const s = amount.toString();
7 years ago
return `${s} ${coinInfo.shortcut}`;
};
7 years ago
export const formatTime = (n: number): string => {
const hours = Math.floor(n / 60);
const minutes = n % 60;
7 years ago
if (!n) return 'No time estimate';
let res = '';
if (hours !== 0) {
res += `${hours} hour`;
7 years ago
if (hours > 1) {
res += 's';
}
res += ' ';
}
if (minutes !== 0) {
res += `${minutes} minutes`;
7 years ago
}
return res;
};
7 years ago
export const btckb2satoshib = (n: number): number => Math.round(n * 1e5);
7 years ago
export const stringToHex = (str: string): string => {
let result: string = '';
let hex: string;
7 years ago
for (let i = 0; i < str.length; i++) {
hex = str.charCodeAt(i).toString(16);
result += (`000${hex}`).slice(-4);
7 years ago
}
return result;
};
7 years ago
export const hexToString = (hex: string): string => {
let str = '';
for (let i = 0; i < hex.length; i += 2) {
const v = parseInt(hex.substr(i, 2), 16);
7 years ago
if (v) str += String.fromCharCode(v);
}
return str;
};
export const toDecimalAmount = (amount: string | number, decimals: number): string => {
try {
return new BigNumber(amount).div(10 ** decimals).toString(10);
} catch (error) {
return '0';
}
};
export const fromDecimalAmount = (amount: string | number, decimals: number): string => {
try {
return new BigNumber(amount).times(10 ** decimals).toString(10);
} catch (error) {
return '0';
}
};