globalizejs / globalize

A JavaScript library for internationalization and localization that leverages the official Unicode CLDR JSON data
https://globalizejs.com
MIT License
4.8k stars 603 forks source link

Number parser bug for en-IN (actually *-IN) #660

Closed rxaviers closed 7 years ago

rxaviers commented 7 years ago

Reported via http://stackoverflow.com/questions/41221215/globalize-bug-in-new-v1-2-1-parsing-certain-numbers-returns-nan-for-17-cultures


With the new version 1.2.1 of Globalize we're seeing 17 cultures for which parsing fails for numbers in the "hundreds" range (e.g. between 100 and 999).

Here's a concrete example using the CLDR JSON data version 30.0.2 (also tested with the CLDR 30.0.3 data with the same results):

var g = new Globalize("en-IN");
g.numberParser()(g.numberFormatter()(1000.01)); // prints 1000.01
g.numberParser()(g.numberFormatter()(999.99)); // prints NaN

To the best of my knowledge, formatting the number 999.99 and then parsing it should not yield NaN, so this looks like a bug. NaN is also the result for the number 100, and indeed it seems like it's the result for any number with three significant digits.

If we just focus on the parsing step, we see the following:

var g = new Globalize("en-IN");
console.log(g.numberParser()("1,000.01")) // prints 1000.01
console.log(g.numberParser()("100.01")) // prints NaN

If we use the earlier Globalize code from version 1.1.2 then the behavior is correct (i.e. no NaN problems).

The 17 cultures in question are: as-IN,bn-BD,bn-IN,brx-IN,dz-BT,en-IN,gu-IN,hi-IN,kok-IN,ks-Arab-IN,ml-IN,mr-IN,or-IN,pa-IN,ta-IN,ta-LK,te-IN. Our unit tests picked them all up after we upgraded to Globalize 1.2.1.

We're wondering if somebody might be kind enough confirm that they can reproduce this (is there a jsfiddle template for doing this sort of thing?), and which change in the new version of Globalize (assuming it is a problem in Globalize) is causing this so that we can perhaps post a corresponding issue.

rxaviers commented 7 years ago

The bug seems related to parsing a number that has 2nd grouping separator. parsing 1,000 works, but 1000 doesn't.

rxaviers commented 7 years ago

Due to the same root cause, Globalize('en').parseNumber('1', {style: 'percent'}) was wrongly returning 1 instead of NaN.