-
Notifications
You must be signed in to change notification settings - Fork 526
Open
Description
The current implementation of the unformat function does not take into account that there are currency symbols that may contain the same character used for the decimal separator.
Please consider the Swedish Krona as an example. That currency uses kr as a currency symbol but most stores use the alternative kr. version (with a trailing dot).
Now, if you try to unformat kr. 123.45, this first replace call returns '.123.45'. When this string is passed to parseFloat, this is what you get:
const regex = new RegExp("[^0-9-" + decimal + "]", ["g"]);
let priceString = 'kr. 123.45';
priceString = priceString.replace( regex, '' );
// price string is now '.123.45'
const result = parseFloat( priceString );
// result is .123 instead of the expected value of 123.45
// because of the leading dot left from the currency symbolThe simplest approach to solve this issue is to pass the currency symbol to unformat so that it can be replaced before going ahead with any further string manipulation. The new function definition could be:
const unformat = function( value, decimal, symbol ) {
symbol = symbol || lib.settings.currency.symbol;
value = value.replaceAll( symbol, '' );
// the rest of the current implementation
}I hope this helps.
Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
No labels