1
0
mirror of https://github.com/MikeMcl/decimal.js.git synced 2024-09-28 22:40:48 +00:00

Add optional decimal separator to "toFormat"

Resolves MikeMcl/decimal.js#6
This commit is contained in:
Joel Santirso 2014-06-30 19:37:37 +02:00
parent e49ebd094d
commit 4bfa454944

View File

@ -1268,17 +1268,18 @@
* [sep1] {string} The grouping separator of the integer part of the number. * [sep1] {string} The grouping separator of the integer part of the number.
* [sep2] {string} The grouping separator of the fraction part of the number. * [sep2] {string} The grouping separator of the fraction part of the number.
* [dp] {number} Decimal places. Integer, -MAX_DIGITS to MAX_DIGITS inclusive. * [dp] {number} Decimal places. Integer, -MAX_DIGITS to MAX_DIGITS inclusive.
* [dsep] {string} Decimal separator, if left undefined it defaults to '.'.
* *
* Non-breaking thin-space: \u202f * Non-breaking thin-space: \u202f
* *
* If dp is invalid the error message will incorrectly give the method as toFixed. * If dp is invalid the error message will incorrectly give the method as toFixed.
* *
*/ */
P['toFormat'] = function ( sep1, dp, sep2 ) { P['toFormat'] = function ( sep1, dp, sep2, dsep ) {
var arr = this.toFixed(dp).split('.'); var arr = this.toFixed(dp).split('.');
return arr[0].replace( /\B(?=(\d{3})+$)/g, sep1 == null ? ',' : sep1 + '' ) + return arr[0].replace( /\B(?=(\d{3})+$)/g, sep1 == null ? ',' : sep1 + '' ) +
( arr[1] ? '.' + ( sep2 ? arr[1].replace( /\d{5}\B/g, '$&' + sep2 ) : arr[1] ) : '' ); ( arr[1] ? (dsep == null ? '.' : dsep) + ( sep2 ? arr[1].replace( /\d{5}\B/g, '$&' + sep2 ) : arr[1] ) : '' );
}; };