Change default precision to undefined for BigNumber formatting too. See #676

This commit is contained in:
jos 2018-01-24 14:44:36 +01:00
parent 0406b93fc6
commit 879e00aecd
4 changed files with 15 additions and 20 deletions

View File

@ -38,10 +38,10 @@ function factory (type, config, load, typed) {
* - `precision: number`
* A number between 0 and 16 to round the digits of the number. In case
* of notations 'exponential' and 'auto', `precision` defines the total
* number of significant digits returned and is undefined by default.
* number of significant digits returned.
* In case of notation 'fixed', `precision` defines the number of
* significant digits after the decimal point, and is undefined by default,
* not rounding any digits.
* significant digits after the decimal point.
* `precision` is undefined by default.
* - `exponential: Object`
* An object containing two parameters, {number} lower and {number} upper,
* used by notation 'auto' to determine when to return exponential

View File

@ -29,14 +29,12 @@
* the digits of the number.
* In case of notations 'exponential' and
* 'auto', `precision` defines the total
* number of significant digits returned
* and is undefined by default.
* number of significant digits returned.
* In case of notation 'fixed',
* `precision` defines the number of
* significant digits after the decimal
* point, and is undefined by default,
* not rounding any digits.
* point, and is 0 by default.
* point.
* `precision` is undefined by default.
* {Object} exponential An object containing two parameters,
* {number} lower and {number} upper,
* used by notation 'auto' to determine
@ -176,11 +174,9 @@ exports.toExponential = function (value, precision) {
/**
* Format a number with fixed notation.
* @param {BigNumber} value
* @param {number} [precision=0] Optional number of decimals after the
* decimal point. Zero by default.
* @param {number} [precision=undefined] Optional number of decimals after the
* decimal point. Undefined by default.
*/
exports.toFixed = function (value, precision) {
return value.toFixed(precision || 0);
// Note: the (precision || 0) is needed as the toFixed of BigNumber has an
// undefined default precision instead of 0.
return value.toFixed(precision);
};

View File

@ -75,14 +75,13 @@ exports.sign = Math.sign || function(x) {
* the digits of the number.
* In case of notations 'exponential' and
* 'auto', `precision` defines the total
* number of significant digits returned
* and is undefined by default.
* number of significant digits returned.
* In case of notation 'fixed',
* `precision` defines the number of
* significant digits after the decimal
* point, and is undefined by default,
* point.
* `precision` is undefined by default,
* not rounding any digits.
* point, and is 0 by default.
* {Object} exponential An object containing two parameters,
* {number} lower and {number} upper,
* used by notation 'auto' to determine

View File

@ -189,8 +189,8 @@ describe('format', function () {
notation: 'fixed'
};
assert.deepEqual(formatter.format(new BigNumber('1.23456'), options), '1');
assert.deepEqual(formatter.format(new BigNumber('1.7'), options), '2');
assert.deepEqual(formatter.format(new BigNumber('1.23456'), options), '1.23456');
assert.deepEqual(formatter.format(new BigNumber('1.7'), options), '1.7');
assert.deepEqual(formatter.format(new BigNumber('12345678'), options), '12345678');
assert.deepEqual(formatter.format(new BigNumber('12e18'), options), '12000000000000000000');
assert.deepEqual(formatter.format(new BigNumber('12e30'), options), '12000000000000000000000000000000');
@ -219,7 +219,7 @@ describe('format', function () {
it('should format a bignumber using toFixed', function() {
var Big = BigNumber.clone({precision: 100});
assert.equal(formatter.toFixed(new Big(2.34)), '2');
assert.equal(formatter.toFixed(new Big(2.34)), '2.34');
assert.equal(formatter.toFixed(new Big(2.34), 1), '2.3');
assert.equal(formatter.toFixed(new Big(2), 20), '2.00000000000000000000');
assert.equal(formatter.toFixed(new Big(2), 21), '2.000000000000000000000');