Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

unformat() returns the wrong value if currency symbol contains the decimal separator #240

Open
LuigiPulcini opened this issue Jul 24, 2023 · 0 comments

Comments

@LuigiPulcini
Copy link

LuigiPulcini commented Jul 24, 2023

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 symbol

The 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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant