react-currency-input icon indicating copy to clipboard operation
react-currency-input copied to clipboard

Negative value do not work for some locale

Open jhasselgren opened this issue 6 years ago • 0 comments

Hi,

I have a problem where the state is not calculated correct for negative values

image

I have found that it is the following code in index.js that is the problem

initialValue = Number(initialValue).toLocaleString(undefined, {
                style                : 'decimal',
                minimumFractionDigits: props.precision,
                maximumFractionDigits: props.precision
            })

The minus sign is change to "−" (‎2212 MINUS SIGN) instead of "-" (‎002D HYPHEN-MINUS)

This result in that the logic for the following line fails

let negativeSignCount = (value.match(/-/g) || []).length;

Because it is looking for "-" (‎002D HYPHEN-MINUS), and in my case where the char is "−" (‎2212 MINUS SIGN) negativeSignCount equals 0, instead of 1

The solution should be to change:

initialValue = Number(initialValue).toLocaleString(undefined, {
                style                : 'decimal',
                minimumFractionDigits: props.precision,
                maximumFractionDigits: props.precision
            })

to

initialValue = Number(initialValue).toFixed(props.precision);

jhasselgren avatar Mar 13 '18 21:03 jhasselgren