How to format BigNumbers.js to min and max decimals

93 views Asked by At

I would like to specify min and max decimal places for the toFormat method in bignumber.js. https://github.com/MikeMcl/bignumber.js/

Consider the following code:

format = {
    prefix              : '',
    decimalSeparator    : ',',
    groupSeparator      : '',
    groupSize           : 3,
    suffix              : ''
};
console.log ( BigNumber( '15.53000' ).toFormat( 6, format ) ); // 15.530000

Nothing wrong here, but I would like to specify min and max decimals and also be able to define a format.

console.log ( BigNumber( '15.53000' ).toFormat( format, 0, 6 ) ); // 15.53
console.log ( BigNumber( '15.53000' ).toFormat( format, 3, 6 ) ); // 15.530
console.log ( BigNumber( '15.53000' ).toFormat( format, 0, 0 ) ); // 16
console.log ( BigNumber( '15.53000' ).toFormat( format, 6, 6 ) ); // 15.530000

Even better would be if you could add two new properties to format (minDecimals, maxDecimals) and remove the numbers from the call:

format = {
    prefix              : '',
    decimalSeparator    : ',',
    groupSeparator      : '',
    groupSize           : 3,
    suffix              : ''
    minDecimals         : 0,
    maxDecimals         : 6
};
console.log ( BigNumber( '15.53000' ).toFormat( format ) ); // 15.53

How could I rewrite the .toFormat method to achieve this?

0

There are 0 answers