Skip to content

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

Description

@LuigiPulcini

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.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions