2018-06-07 16:05:22 +00:00
|
|
|
/** @format */
|
|
|
|
/**
|
2018-06-20 17:43:53 +00:00
|
|
|
* External dependencies
|
2018-06-07 16:05:22 +00:00
|
|
|
*/
|
2018-06-20 17:43:53 +00:00
|
|
|
import { isNaN } from 'lodash';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Formats money with a given currency code. Uses site's current locale for symbol formatting
|
|
|
|
*
|
|
|
|
* @see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/NumberFormat
|
|
|
|
* @param {Number|String} number number to format
|
|
|
|
* @param {String} currency currency code e.g. 'USD'
|
|
|
|
* @returns {?String} A formatted string.
|
|
|
|
*/
|
|
|
|
export function formatCurrency( number, currency ) {
|
|
|
|
const locale = wcSettings.locale || 'en-US'; // Default so we don't break.
|
|
|
|
if ( 'number' !== typeof number ) {
|
|
|
|
number = parseFloat( number );
|
|
|
|
}
|
|
|
|
if ( isNaN( number ) ) {
|
|
|
|
return '';
|
|
|
|
}
|
|
|
|
return new Intl.NumberFormat( locale, { style: 'currency', currency } ).format( number );
|
|
|
|
}
|
2018-06-07 16:05:22 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the rounded decimal value of a number at the precision used for a given currency.
|
|
|
|
* This is a work-around for fraction-cents, meant to be used like `wc_format_decimal`
|
|
|
|
*
|
|
|
|
* @param {Number|String} number A floating point number (or integer), or string that converts to a number
|
|
|
|
* @return {Number} The original number rounded to a decimal point
|
|
|
|
*/
|
2018-06-20 17:43:53 +00:00
|
|
|
export function getCurrencyFormatDecimal( number ) {
|
|
|
|
const { precision = 2 } = wcSettings.currency;
|
2018-06-07 16:05:22 +00:00
|
|
|
if ( 'number' !== typeof number ) {
|
|
|
|
number = parseFloat( number );
|
|
|
|
}
|
|
|
|
if ( isNaN( number ) ) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
return Math.round( number * Math.pow( 10, precision ) ) / Math.pow( 10, precision );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2018-06-20 17:43:53 +00:00
|
|
|
* Get the string representation of a floating point number to the precision used by the current currency.
|
2018-06-07 16:05:22 +00:00
|
|
|
* This is different from `formatCurrency` by not returning the currency symbol.
|
|
|
|
*
|
2018-06-20 17:43:53 +00:00
|
|
|
* @param {Number|String} number A floating point number (or integer), or string that converts to a number
|
|
|
|
* @return {String} The original number rounded to a decimal point
|
2018-06-07 16:05:22 +00:00
|
|
|
*/
|
2018-06-20 17:43:53 +00:00
|
|
|
export function getCurrencyFormatString( number ) {
|
|
|
|
const { precision = 2 } = wcSettings.currency;
|
2018-06-07 16:05:22 +00:00
|
|
|
if ( 'number' !== typeof number ) {
|
|
|
|
number = parseFloat( number );
|
|
|
|
}
|
|
|
|
if ( isNaN( number ) ) {
|
|
|
|
return '';
|
|
|
|
}
|
|
|
|
return number.toFixed( precision );
|
|
|
|
}
|