openexchangerates / accounting.js

A lightweight JavaScript library for number, money and currency formatting - fully localisable, zero dependencies.
http://openexchangerates.github.io/accounting.js
MIT License
4.95k stars 528 forks source link

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

Open LuigiPulcini opened 11 months ago

LuigiPulcini commented 11 months ago

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.