Globalize.parseFloat does not consistently parse group and decimal separators
I made some tests and i think is stm wrong with Globalize.parseFloat. Look code below:
Globalize.culture("pl");
//console.log(Globalize.culture.name)
// test the "n" command
console.log(Globalize.format(1839.560, "n1")); // outputs 1.839,6
console.log(Globalize.format(1839.560, "n0")); // outputs 1.840
console.log(Globalize.format(1839.560, "n6")); // outputs 1.839,560000
// test the "c" command
console.log(Globalize.format(1839.560, "c2")); // outputs 1.839,56 €
console.log(Globalize.format(1839.560, "c3")); // outputs 1.839,560 €
console.log(Globalize.format(1839.560, "c6")); // outputs 1.839,560000 €
// assuming a culture with "/" as the date separator symbol
console.log(Globalize.format(new Date(1955, 10, 5)), "yyyy/MM/dd"); // "1955/11/05"
console.log(Globalize.format(new Date(1955, 10, 5)), "dddd MMMM d, yyyy"); // "Saturday November 5, 1955"
// assuming a culture where "," is the group separator
// and "." is the decimal separator
console.log(Globalize.parseInt(Globalize.format(1839.560, "n3"))); // 1234
// assuming a culture where "." is the group separator
// and "," is the decimal separator
console.log(Globalize.parseInt("1234,56")); // 1234
// assuming a culture where "," is the group separator
// and "," is the decimal separator
console.log(Globalize.parseFloat("1839,560")); // 1234.56
// assuming a culture where "." is the group separator
// and "." is the decimal separator
console.log(Globalize.parseFloat("1234.56")); // 1234.56
and all is fine excepts Parse float which return always the same values with '.' instead of ',' .
here is console log:
Dziennik: 1 839,6 Dziennik: 1 840 Dziennik: 1 839,560000 Dziennik: 1 839,56 zł Dziennik: 1 839,560 zł Dziennik: 1 839,560000 zł Dziennik: 5 listopada 1955 00:00:00yyyy/MM/dd Dziennik: 5 listopada 1955 00:00:00dddd MMMM d, yyyy Dziennik: 1839 Dziennik: 1234 Dziennik: 1839.56 Dziennik: 1234.56
Maybe I do sth wrong ?
It does not seem reasonable or necessary to support a culture that uses the same symbol for both the group separator and the decimal separator. Am I understanding right, that's what you're doing? Does such a culture exist? If so, how would it be expected to work?
Look on this: 1234,56 (good for polish lang.) 1234.56 (bad there not exist '.' in polish numbers) 1 234,56(good but space should disappear after Parse) 1.234,56( bad there '.' not exist maybe should change '.' on empty)
I think i expect working like in C# Parse. Maybe i miss the point but when i use validator in MVC applications this make a little hole in client validation:
$.validator.methods.number = function (value, element) {
console.log(this.optional(element));
console.log(Globalize.parseFloat(value));
console.log(!isNaN(Globalize.parseFloat(value)));
return this.optional(element) || !isNaN(Globalize.parseFloat(value));
}
In field i expect see example: 1234,56 and validator should say: I'ts OK but if someone write 1234.56 its error because in polish we don't use dot '.' so should be NaN. Of course server validation will reject but still i miss sth :)
Ok, I understand now
// Native equal( parseFloat( "500,600" ), 500 ) // Pass equal( parseFloat( "500a600" ), 500 ) // Pass
// Globalize - using "en" culture that has a "," group separator and a "." decimal separator Globalize.culture( "en" ); equal( Globalize.parseFloat( "500 600" ), 500 ); // Fail: NaN equal( Globalize.parseFloat( "500a600" ), 500 ); // Fail: NaN
// Globalize - using "fr" culture that has a " " group separator and a "," decimal separator Globalize.culture( "fr" ); equal( Globalize.parseFloat("500.600"), 500 ); // Fail: 500.6 equal( Globalize.parseFloat("500a600"), 500 ); // Fail: NaN
Yes
in my case will be sth like this (or should be).
Globalize.culture("pl"); console.log(Globalize.parseInt("1234,56")); // 1234 -> good console.log(Globalize.parseInt("1234.56")); // 1234 -> wrong '.' is incorrect sign in pl console.log(Globalize.parseInt("1 234,56")); // 1234 -> good because ' ' and ',' exist in pl console.log(Globalize.parseInt("1 234.56")); // 1234 -> wrong '.' dot problem console.log(Globalize.parseFloat("1234,56")); // 1234.56 -> wrong should be 1234,56 console.log(Globalize.parseFloat("1234.56")); // 1234.56 -> wrong '.' dot problem
There is another problem i think is if u return example console.log(Globalize.parseFloat("1234.56")); // 123,56 -> should be wrong because '.' dot (in PL ofc)
I have hope this can work like we speak :) Greatings.
The key problem is that Globalize.parseFloat (and hence Globalize.parseInt, which just calls it and truncates) mainly just manipulates the input by replacing localized separators so that the number is in standard JavaScript format, then calls the standard parseFloat. So if the input is 1234,56 and the comma is defined as the decimal separator, the input is converted to 1234.56 and then parsed with parseFloat. But if the input is 1234.56, it does not get modified unless the period is a thousands separator (in which case it is removed).
This has previously been discussed in Issue #46 (which is originally about a different problem).
It’s not ever clear whether this should be fixed. It may be desirable to accept both 1234,56 and 1234.56, as people often expect that when using computers, the desimal separator needs to be the period. Non-localized numeric input is so common that it affects users' expectations and habits.
It's different with a culture that uses the period as thousands separator - then 123.456 gets converted to 123456 which is ok, though 1234.56 too gets converted to 123456 which is wrong.
Look on this example of code:
$.extend(jQuery.validator.methods, { range: function (value, element, param) { //Use the Globalization plugin to parse the value var val = $.global.parseFloat(value); return this.optional(element) || (val >= param[0] && val <= param[1]); } });
Tell me where u end with yr way of thinking ... or beter is here where u expect error (consider polish language wit numbers like 1234,56) :)
$.validator.methods.number = function (value, element) {
return this.optional(element) || !isNaN(Globalize.parseFloat(value));
}
This is definitely an issue for me right now with an application I am writing that has to support 14 different cultures starting with German and US English. A German user inputs 5,1 and expects that to be treated the same as 5.1 for a en-US user. Instead I get 51. Any help would be appreciated. Do we have an update? Is it still your opinion that this not something that should be addressed or at least "maybe" shouldn't be addressed?
The same problem with the thousands separator is apparent in the Dutch culture (nl-NL). It seems that parseFloat ignores the group size. The code fragment below fixes this. Would you consider merging this into your code base?
// handle groups (e.g. 1,000,000)
var groupSep = nf[","];
var groups = integer.split(groupSep);
if (groups.length > 1) {
// if the digits are grouped, check if the grouping is valid.
var i, groupLength, groupSize;
for (i = 0; i < groups.length; i++) {
groupLength = groups[groups.length - i - 1].length;
if (groupLength == 0) {
// empty groups are not allowed
return ret;
}
groupSize = nf.groupSizes[Math.min(i, nf.groupSizes.length - 1)];
if (i == groups.length - 1) {
// the last group should not be longer than the group size (unless its 0 meaning no more separators from here).
if (groupSize > 0 && groupLength > groupSize) {
return ret;
}
}
else {
if (groupLength != groupSize) {
// invalid when incorrect group size
return ret;
}
}
}
}
integer = groups.join("");
rancomarcus thanks. It seems that this fixes the bug for Spanish culture as well. It seems incredible that even we do not have simple and reliable methods of making web applications globally. Thank you very much again.
Can confirm it works with danish culture as well, good work :-)
I have this problem with Serbian culture as well, parseFloat doesnt make a difference between thousands separator and decimal separator. Any chance of correcting this?
Is there still discussion about how this issue should be resolved? Would the code snippet above be included in the master if I create a pull request?
If there's discussion, this is the place for it.
Does the code snippet above pass the unit tests I posted further above? That seems a decent first step.
If you replace the line integer = integer.split( groupSep ).join( "" ); with the code fragment above, the test below all pass.
module("instance", lifecycle );
test("constructor sets culture", function() {
var globalizeDe = Globalize("de");
equal( Globalize.parseFloat("5.3"), 5.3 );
ok( isNaN( Globalize.parseFloat("5,3")));
ok( isNaN( Globalize.parseFloat("5.3", "de")));
equal( Globalize.parseFloat("5,3", "de"), 5.3 );
ok( isNaN( globalizeDe.parseFloat("5.3")));
equal( globalizeDe.parseFloat("5,3"), 5.3 );
equal( globalizeDe.parseFloat("5.3", "en"), 5.3 );
ok( isNaN(globalizeDe.parseFloat("5,3", "en")));
// Native
equal( parseFloat( "500,600" ), 500 ) // Pass
equal( parseFloat( "500a600" ), 500 ) // Pass
// Globalize - using "en" culture that has a "," group separator and a "." decimal separator
Globalize.culture( "en" );
ok( isNaN( Globalize.parseFloat( "500 600" ))); // Fail: NaN
ok( isNaN( Globalize.parseFloat( "500a600" ))); // Fail: NaN
// Globalize - using "fr" culture that has a " " group separator and a "," decimal separator
Globalize.culture( "fr" );
notEqual( Globalize.parseFloat("500.600"), 500 ); // Fail: 500.6
notEqual( Globalize.parseFloat("500a600"), 500 ); // Fail: NaN
});
Feel free to do a pull request (include tests please). Not sure when it may get reviewed, but it's at least a step closer to what we have now. Thanks.
I tried to fork jquery/globalize, but I get a HTTP 404 error. Not sure what I'm doing wrong.
From: Richard D. Worth [[email protected]] Sent: Thursday, March 15, 2012 2:19 PM To: Ranco Marcus Subject: Re: [globalize] Globalize.parseFloat does not consistently parse group and decimal separators (#73)
Feel free to do a pull request (include tests please). Not sure when it may get reviewed, but it's at least a step closer to what we have now. Thanks.
Reply to this email directly or view it on GitHub: https://github.com/jquery/globalize/issues/73#issuecomment-4519327
Sounds like possibly an issue with github.com. Suggest you try a bit later and if it still doesn't work, report it at https://github.com/contact
There seemed to be a problem with Github's logon mechanism. It is fixed now and I have prepared a pull request for this issue.
+1 adogg. Issue is still reproduceable for Russia culture
On Globalize 1.x, there's one only method to format Numbers (integers or floats): .formatNumber(). This method has been completely rewritten from previous Globalize and it seems to behave correctly.
In English:
> Globalize("en").formatNumber(12345678.05)
'12,345,678.05'
> Globalize("en").parseNumber('12,345,678.05')
12345678.05
In Polish:
> Globalize("pl").formatNumber(12345678.05);
'12 345 678,05'
> Globalize("pl").parseNumber('12 345 678,05')
12345678.05
Feel free to try any other locales or tests and add new comments in case there's a bug.