parseFloat gives NaN for swedish culture

1.6k Views Asked by At

I am using jQuery Globalize plugin along with jQuery validation.

For the valid swedish number 5.000,00 parseFloat function gives NaN

What could be the bug in below code/library?

<script type="text/javascript" src="/Scripts/jquery-1.9.0.min.js"></script>
<script type="text/javascript" src="/Scripts/globalize.js"></script>
<script type="text/javascript" src="/Scripts/cultures/globalize.culture.sv.js"></script>
<script type="text/javascript" src="/Scripts/jquery.validate.js"></script>
<script type="text/javascript" src="/Scripts/jquery.validate.unobtrusive.js" ></script>

<script type="text/javascript">

 jQuery(function () {

        Globalize.culture('sv');

        $.validator.methods.number = function (value, element) {

            if (Globalize.parseFloat(value)) { // this gives NaN

                return true;
            }
            return false;
        }
    });
   </script>
2

There are 2 best solutions below

0
On

Sweden actually use space as thousand delimiter. I cant find a really good reference for it though. (I am Swedish and 1 000 000,123 looks better to me than 1.000.000,123)

the following .NET C# Code seams to confirm it.

Console.WriteLine(String.Format("'{0}'", System.Globalization.CultureInfo.GetCultureInfo("sv-SE").NumberFormat.NumberGroupSeparator));

Also if you go to 'Region and language' and then 'additional settings' in windows and select Swedish (Sweden) it shows Digit grouping symbol as a space.

And oracle seams to agree: http://docs.oracle.com/cd/E19455-01/806-0169/overview-9/index.html

1
On

I think that you are using wrong culture. Or your input number is just in wrong format. http://jsfiddle.net/98sgm/1/

jQuery(function () {
    Globalize.culture('sv');
    alert(Globalize.parseFloat('5.000,00'));
    alert(Globalize.parseFloat('5,000.00'));
 });

And source code from culture file:

numberFormat: {
    ",": " ",
    ".": ",",
    negativeInfinity: "-INF",
    positiveInfinity: "INF",
    percent: {
        ",": " ",
        ".": ","
    },
    currency: {
        pattern: ["-n $","n $"],
        ",": ".",
        ".": ",",
        symbol: "kr"
    }
},

EDIT: Create new culture file and set numberFormat options to following:

numberFormat: {
    ",": ".",
    ".": ",",
    negativeInfinity: "-INF",
    positiveInfinity: "INF",
    percent: {
        ",": " ",
        ".": ","
    },
    currency: {
        pattern: ["-n $","n $"],
        ",": ".",
        ".": ",",
        symbol: "kr"
    }
},