JSFiddle - React, Tailwind, and code Playground
by helgatheviking
HTML
<script src="https://cdnjs.cloudflare.com/ajax/libs/accounting.js/0.4.1/accounting.min.js"></script>
<input id="input" type="text" value="123.000"></input>
<button>
trim
</button>
<div id="result"></div>
JavaScript
var woocommerce_nyp_params = {
"currency_format_num_decimals": "2",
"currency_format_symbol": "$",
"currency_format_decimal_sep": ".",
"currency_format_thousand_sep": ",",
"currency_format": "%s%v",
"annual_price_factors": {
"day": "365",
"week": "52",
"month": "12",
"year": "1"
},
"i18n_subscription_string": "%price \/ %period",
"trim_trailing_zeros": true
};
$('button').on('click', function(e) {
e.preventDefault();
$('#input').trigger('change');
});
$('#input').on('change', function(e) {
e.preventDefault();
let value = this.value;
value = accounting.unformat(value, woocommerce_nyp_params.currency_format_decimal_sep);
value = accounting.formatMoney(
value, {
symbol: woocommerce_nyp_params.currency_format_symbol,
decimal: woocommerce_nyp_params.currency_format_decimal_sep,
thousand: woocommerce_nyp_params.currency_format_thousand_sep,
precision: woocommerce_nyp_params.currency_format_num_decimals,
format: woocommerce_nyp_params.currency_format
}
)
// This produces "Uncaught SyntaxError: nothing to repeat"
//let regex = new RegExp('\\' + woocommerce_nyp_params.currency_format_decimal_sep + '0++$', 'i');
let regex = new RegExp('\\' + woocommerce_nyp_params.currency_format_decimal_sep + '0+$', 'i');
value = value.replace( regex, '' );
$('#result').text(value);
});