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-26 21:39:29 +00:00
|
|
|
import { get, isNaN } from 'lodash';
|
2018-06-20 17:43:53 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* 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 ) {
|
2018-07-04 01:50:12 +00:00
|
|
|
const locale = wcSettings.siteLocale || 'en-US'; // Default so we don't break.
|
2018-06-26 21:39:29 +00:00
|
|
|
// default to wcSettings if currency is not passed in
|
|
|
|
if ( ! currency ) {
|
|
|
|
currency = get( wcSettings, 'currency.code', 'USD' );
|
|
|
|
}
|
2018-06-20 17:43:53 +00:00
|
|
|
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
|
|
|
|
|
|
|
/**
|
2018-06-20 18:19:58 +00:00
|
|
|
* Get the rounded decimal value of a number at the precision used for the current currency.
|
2018-06-07 16:05:22 +00:00
|
|
|
* 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 );
|
|
|
|
}
|