Skip to content

Instantly share code, notes, and snippets.

@pedrouid pedrouid/bignumber.js
Last active Jun 14, 2019

Embed
What would you like to do?
BigNumber Helper Functions (Javascript & Typescript)
import BigNumber from "bignumber.js";
export function random() {
return BigNumber.random().toString();
}
export function toFixed(value, decimals) {
return new BigNumber(`${value}`).toFixed(decimals).toString();
}
export function isNaN(value) {
return new BigNumber(`${value}`).isNaN();
}
export function isNumber(value) {
const isNaNResult = isNaN(value);
return !isNaNResult;
}
export function isInteger(value) {
return new BigNumber(`${value}`).isInteger();
}
export function isPositive(value) {
return new BigNumber(`${value}`).isPositive();
}
export function isNegative(value) {
return new BigNumber(`${value}`).isNegative();
}
export function isZero(value) {
return new BigNumber(`${value}`).isZero();
}
export function countDecimalPlaces(value) {
return new BigNumber(`${value}`).dp();
}
export function convertNumberToString(value) {
return new BigNumber(`${value}`).toString();
}
export function convertStringToNumber(value) {
return new BigNumber(`${value}`).toNumber();
}
export function convertHexToString(hex) {
return new BigNumber(`${hex}`).toString();
}
export function convertStringToHex(value) {
return new BigNumber(`${value}`).toString(16);
}
export function convertHexToNumber(hex) {
return convertStringToNumber(convertHexToString(hex));
}
export function convertNumberToHex(value) {
return convertStringToHex(convertNumberToString(value));
}
export function greaterThan(numberOne, numberTwo) {
return (
new BigNumber(`${numberOne}`).comparedTo(new BigNumber(`${numberTwo}`)) ===
1
);
}
export function greaterThanOrEqual(numberOne, numberTwo) {
return (
new BigNumber(`${numberOne}`).comparedTo(new BigNumber(`${numberTwo}`)) >= 0
);
}
export function smallerThan(numberOne, numberTwo) {
return (
new BigNumber(`${numberOne}`).comparedTo(new BigNumber(`${numberTwo}`)) ===
-1
);
}
export function smallerThanOrEqual(numberOne, numberTwo) {
return (
new BigNumber(`${numberOne}`).comparedTo(new BigNumber(`${numberTwo}`)) <= 0
);
}
export function multiply(numberOne, numberTwo) {
return new BigNumber(`${numberOne}`)
.times(new BigNumber(`${numberTwo}`))
.toString();
}
export function divide(numberOne, numberTwo) {
return new BigNumber(`${numberOne}`)
.dividedBy(new BigNumber(`${numberTwo}`))
.toString();
}
export function floorDivide(numberOne, numberTwo) {
return new BigNumber(`${numberOne}`)
.dividedToIntegerBy(new BigNumber(`${numberTwo}`))
.toString();
}
export function mod(numberOne, numberTwo) {
return new BigNumber(`${numberOne}`)
.mod(new BigNumber(`${numberTwo}`))
.toString();
}
export function add(numberOne, numberTwo) {
return new BigNumber(`${numberOne}`)
.plus(new BigNumber(`${numberTwo}`))
.toString();
}
export function subtract(numberOne, numberTwo) {
return new BigNumber(`${numberOne}`)
.minus(new BigNumber(`${numberTwo}`))
.toString();
}
export function convertAmountToRawNumber(value, decimals = 18) {
return new BigNumber(`${value}`)
.times(new BigNumber("10").pow(decimals))
.toString();
}
export function convertAmountFromRawNumber(value, decimals = 18) {
return new BigNumber(`${value}`)
.dividedBy(new BigNumber("10").pow(decimals))
.toString();
}
export function handleSignificantDecimals(value, decimals, buffer) {
const result = formatSignificantDecimals(value, decimals, buffer);
return new BigNumber(`${result}`).dp() <= 2
? new BigNumber(`${result}`).toFormat(2)
: new BigNumber(`${result}`).toFormat();
}
export function formatSignificantDecimals(value, decimals, buffer) {
if (
!new BigNumber(`${decimals}`).isInteger() ||
(buffer && !new BigNumber(`${buffer}`).isInteger())
) {
return null;
}
buffer = buffer ? convertStringToNumber(buffer) : 3;
decimals = convertStringToNumber(decimals);
const absolute = new BigNumber(`${value}`).abs().toNumber();
if (smallerThan(absolute, 1)) {
decimals = value.slice(2).search(/[^0]/g) + buffer;
decimals = decimals < 8 ? decimals : 8;
} else {
decimals = decimals < buffer ? decimals : buffer;
}
let result = new BigNumber(`${value}`).toFixed(decimals);
result = new BigNumber(`${result}`).toString();
return result;
}
export function formatFixedDecimals(value, decimals) {
const _value = convertNumberToString(value);
const _decimals = convertStringToNumber(decimals);
const result = new BigNumber(
new BigNumber(_value).toFixed(_decimals)
).toString();
return result;
}
export function formatInputDecimals(inputOne, inputTwo) {
const _nativeAmountDecimalPlaces = countDecimalPlaces(inputTwo);
const decimals =
_nativeAmountDecimalPlaces > 8 ? _nativeAmountDecimalPlaces : 8;
const result = new BigNumber(formatFixedDecimals(inputOne, decimals))
.toFormat()
.replace(/,/g, "");
return result;
}
import BigNumber from "bignumber.js";
export function random(): string {
return BigNumber.random().toString();
}
export function toFixed(value: string | number, decimals: number): string {
return new BigNumber(`${value}`).toFixed(decimals).toString();
}
export function isNaN(value: string | number): boolean {
return new BigNumber(`${value}`).isNaN();
}
export function isNumber(value: string | number): boolean {
const isNaNResult = isNaN(value);
return !isNaNResult;
}
export function isInteger(value: string | number): boolean {
return new BigNumber(`${value}`).isInteger();
}
export function isPositive(value: string | number): boolean {
return new BigNumber(`${value}`).isPositive();
}
export function isNegative(value: string | number): boolean {
return new BigNumber(`${value}`).isNegative();
}
export function isZero(value: string | number): boolean {
return new BigNumber(`${value}`).isZero();
}
export function countDecimalPlaces(value: string | number): number {
return new BigNumber(`${value}`).dp();
}
export function convertNumberToString(value: string | number): string {
return new BigNumber(`${value}`).toString();
}
export function convertStringToNumber(value: string | number): number {
return new BigNumber(`${value}`).toNumber();
}
export function convertHexToString(hex: string): string {
return new BigNumber(`${hex}`).toString();
}
export function convertStringToHex(value: string | number): string {
return new BigNumber(`${value}`).toString(16);
}
export function convertHexToNumber(hex: string): number {
return convertStringToNumber(convertHexToString(hex));
}
export function convertNumberToHex(value: string | number): string {
return convertStringToHex(convertNumberToString(value));
}
export function greaterThan(
numberOne: number | string,
numberTwo: number | string
): boolean {
return (
new BigNumber(`${numberOne}`).comparedTo(new BigNumber(`${numberTwo}`)) ===
1
);
}
export function greaterThanOrEqual(
numberOne: number,
numberTwo: number
): boolean {
return (
new BigNumber(`${numberOne}`).comparedTo(new BigNumber(`${numberTwo}`)) >= 0
);
}
export function smallerThan(
numberOne: number | string,
numberTwo: number | string
): boolean {
return (
new BigNumber(`${numberOne}`).comparedTo(new BigNumber(`${numberTwo}`)) ===
-1
);
}
export function smallerThanOrEqual(
numberOne: number,
numberTwo: number
): boolean {
return (
new BigNumber(`${numberOne}`).comparedTo(new BigNumber(`${numberTwo}`)) <= 0
);
}
export function multiply(
numberOne: number | string,
numberTwo: number | string
): string {
return new BigNumber(`${numberOne}`)
.times(new BigNumber(`${numberTwo}`))
.toString();
}
export function divide(
numberOne: number | string,
numberTwo: number | string
): string {
return new BigNumber(`${numberOne}`)
.dividedBy(new BigNumber(`${numberTwo}`))
.toString();
}
export function floorDivide(
numberOne: number | string,
numberTwo: number | string
): string {
return new BigNumber(`${numberOne}`)
.dividedToIntegerBy(new BigNumber(`${numberTwo}`))
.toString();
}
export function mod(
numberOne: number | string,
numberTwo: number | string
): string {
return new BigNumber(`${numberOne}`)
.mod(new BigNumber(`${numberTwo}`))
.toString();
}
export function add(
numberOne: number | string,
numberTwo: number | string
): string {
return new BigNumber(`${numberOne}`)
.plus(new BigNumber(`${numberTwo}`))
.toString();
}
export function subtract(
numberOne: number | string,
numberTwo: number | string
): string {
return new BigNumber(`${numberOne}`)
.minus(new BigNumber(`${numberTwo}`))
.toString();
}
export function convertAmountToRawNumber(
value: string | number,
decimals: number = 18
): string {
return new BigNumber(`${value}`)
.times(new BigNumber("10").pow(decimals))
.toString();
}
export function convertAmountFromRawNumber(
value: string | number,
decimals: number = 18
): string {
return new BigNumber(`${value}`)
.dividedBy(new BigNumber("10").pow(decimals))
.toString();
}
export function handleSignificantDecimals(
value: string,
decimals: number,
buffer?: number
): string | null {
const result = formatSignificantDecimals(value, decimals, buffer);
return new BigNumber(`${result}`).dp() <= 2
? new BigNumber(`${result}`).toFormat(2)
: new BigNumber(`${result}`).toFormat();
}
export function formatSignificantDecimals(
value: string,
decimals: number,
buffer?: number
): string | null {
if (
!new BigNumber(`${decimals}`).isInteger() ||
(buffer && !new BigNumber(`${buffer}`).isInteger())
) {
return null;
}
buffer = buffer ? convertStringToNumber(buffer) : 3;
decimals = convertStringToNumber(decimals);
const absolute = new BigNumber(`${value}`).abs().toNumber();
if (smallerThan(absolute, 1)) {
decimals = value.slice(2).search(/[^0]/g) + buffer;
decimals = decimals < 8 ? decimals : 8;
} else {
decimals = decimals < buffer ? decimals : buffer;
}
let result = new BigNumber(`${value}`).toFixed(decimals);
result = new BigNumber(`${result}`).toString();
return result;
}
export function formatFixedDecimals(value: string, decimals: number): string {
const _value = convertNumberToString(value);
const _decimals = convertStringToNumber(decimals);
const result = new BigNumber(
new BigNumber(_value).toFixed(_decimals)
).toString();
return result;
}
export function formatInputDecimals(
inputOne: string,
inputTwo: string
): string {
const _nativeAmountDecimalPlaces = countDecimalPlaces(inputTwo);
const decimals =
_nativeAmountDecimalPlaces > 8 ? _nativeAmountDecimalPlaces : 8;
const result = new BigNumber(formatFixedDecimals(inputOne, decimals))
.toFormat()
.replace(/,/g, "");
return result;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.