Simplify precision preservation logic in numberFormat().
Props @Aljullu.
This commit is contained in:
parent
7a0243e6d7
commit
2ad5922f1f
|
@ -8,7 +8,7 @@ const number_format = require( 'locutus/php/strings/number_format' );
|
|||
/**
|
||||
* Formats a number using site's current locale
|
||||
*
|
||||
* @see https://locutus.io/php/strings/number_format/
|
||||
* @see http://locutus.io/php/strings/number_format/
|
||||
* @param {Number|String} number number to format
|
||||
* @param {int|null} [precision=null] optional decimal precision
|
||||
* @returns {?String} A formatted string.
|
||||
|
@ -25,21 +25,11 @@ export function numberFormat( number, precision = null ) {
|
|||
|
||||
const decimalSeparator = get( wcSettings, [ 'currency', 'decimal_separator' ], '.' );
|
||||
const thousandSeparator = get( wcSettings, [ 'currency', 'thousand_separator' ], ',' );
|
||||
precision = parseInt( precision );
|
||||
|
||||
if ( null === precision ) {
|
||||
const [ numberNoDecimals, decimals ] = number.toString().split( '.' );
|
||||
const formattedNumber = number_format(
|
||||
numberNoDecimals,
|
||||
0,
|
||||
decimalSeparator,
|
||||
thousandSeparator
|
||||
);
|
||||
|
||||
if ( decimals ) {
|
||||
return formattedNumber + decimalSeparator + decimals;
|
||||
}
|
||||
|
||||
return formattedNumber;
|
||||
if ( isNaN( precision ) ) {
|
||||
const [ , decimals ] = number.toString().split( '.' );
|
||||
precision = decimals ? decimals.length : 0;
|
||||
}
|
||||
|
||||
return number_format( number, precision, decimalSeparator, thousandSeparator );
|
||||
|
|
Loading…
Reference in New Issue