Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enhancement: Add support for custom minus sign in toFormat method #289

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions bignumber.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,8 @@ export namespace BigNumber {
* FORMAT: {
* // string to prepend
* prefix: '',
* // string sign for negative value
* minusSign: '-',
* // the decimal separator
* decimalSeparator: '.',
* // the grouping separator of the integer part
Expand Down Expand Up @@ -285,6 +287,9 @@ export namespace BigNumber {
/** The string to prepend. */
prefix?: string;

/** The string sign for negative value. */
minusSign?: string;

/** The decimal separator. */
decimalSeparator?: string;

Expand Down
13 changes: 9 additions & 4 deletions bignumber.js
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,7 @@
// The format specification used by the BigNumber.prototype.toFormat method.
FORMAT = {
prefix: '',
minusSign: '-',
groupSize: 3,
secondaryGroupSize: 0,
groupSeparator: ',',
Expand Down Expand Up @@ -409,6 +410,7 @@
* not contain '.'.
* FORMAT {object} An object with some of the following properties:
* prefix {string}
* minusSign {string}
* groupSize {number}
* secondaryGroupSize {number}
* groupSeparator {string}
Expand Down Expand Up @@ -2517,6 +2519,7 @@
*
* FORMAT = {
* prefix: '',
* minusSign: '-',
* groupSize: 3,
* secondaryGroupSize: 0,
* groupSeparator: ',',
Expand All @@ -2535,6 +2538,7 @@
*/
P.toFormat = function (dp, rm, format) {
var str,
isNeg,
x = this;

if (format == null) {
Expand All @@ -2553,7 +2557,9 @@
}

str = x.toFixed(dp, rm);

isNeg = x.s < 0;
str = isNeg ? str.slice(1) : str;

if (x.c) {
var i,
arr = str.split('.'),
Expand All @@ -2562,8 +2568,7 @@
groupSeparator = format.groupSeparator || '',
intPart = arr[0],
fractionPart = arr[1],
isNeg = x.s < 0,
intDigits = isNeg ? intPart.slice(1) : intPart,
intDigits = intPart,
len = intDigits.length;

if (g2) i = g1, g1 = g2, g2 = i, len -= i;
Expand All @@ -2573,7 +2578,6 @@
intPart = intDigits.substr(0, i);
for (; i < len; i += g1) intPart += groupSeparator + intDigits.substr(i, g1);
if (g2 > 0) intPart += groupSeparator + intDigits.slice(i);
if (isNeg) intPart = '-' + intPart;
}

str = fractionPart
Expand All @@ -2583,6 +2587,7 @@
: fractionPart)
: intPart;
}
if (isNeg) str = (format.minusSign || '-') + str;

return (format.prefix || '') + str + (format.suffix || '');
};
Expand Down
2 changes: 1 addition & 1 deletion bignumber.min.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion bignumber.min.js.map

Large diffs are not rendered by default.

2 changes: 2 additions & 0 deletions doc/API.html
Original file line number Diff line number Diff line change
Expand Up @@ -595,6 +595,8 @@ <h5 id="config">config<code class='inset'>set([object]) <i>&rArr; object</i></co
FORMAT: {
// string to prepend
prefix: '',
// string sign of negative value
minusSign: '-',
// decimal separator
decimalSeparator: '.',
// grouping separator of the integer part
Expand Down
16 changes: 16 additions & 0 deletions test/methods/toFormat.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ Test('toFormat', function () {
}

var format = {
minusSign: '-',
decimalSeparator: '.',
groupSeparator: ',',
groupSize: 3,
Expand Down Expand Up @@ -68,6 +69,21 @@ Test('toFormat', function () {

t('76,852.342091', '7.6852342091e+4');

format.minusSign = '−';

t('1', 1);
t('−1', -1);
t('123.456', 123.456);
t('−123.456', -123.456);
t('76,852.342091', '7.6852342091e+4');
t('−76,852.342091', '-7.6852342091e+4');
t('999,999,999,999,999', 999999999999999);
t('−999,999,999,999,999', -999999999999999);
t('NaN', NaN);
t('Infinity', 1/0);
t('−Infinity', -1/0);

format.minusSign = '-';
format.groupSeparator = ' ';

t('76 852.34', '7.6852342091e+4', 2);
Expand Down
2 changes: 1 addition & 1 deletion test/methods/toString.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ Test('toString', function () {
ROUNDING_MODE: 4,
RANGE: 1E9,
EXPONENTIAL_AT: 1E9,
ALPHABET: '0123456789abcdefghijklmnopqrstuvwxyz'
ALPHABET: '0123456789abcdefghijklmnopqrstuvwxyz',
});

t('NaN', NaN);
Expand Down