Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the best way to parse localized numbers from .NET/Razor in javascript?

I have a .NET app that allows a user to choose their own language & culture (date/number formatting). Their culture setting is stored in Thread.CurrentThread.CurrentCulture (also Thread.CurrentThread.CurrentUICulture, but that's a separate issue).

When I print out a var via Razor, it shows in localized format:

<span>@bignum</span> (renders as "123.456" or "123,456")

However, I also need to pass some .NET vars to Javascript:

var js_bignum = @bignum;

The problem is that Javascript in this case does not understand the localized versions of these numbers, so it fails since the above statement becomes:

var js_bignum = 123,456;

It may be because the user's browser's culture setting is different from the user's webapp's culture setting. At any rate, it's a situation we need to be able to handle.

So what's the easiest way to handle this? I can create my own Javascript ConvertToStandardNumberFormat() that takes a string value from .NET and returns a "standard" number format, but that seems like a bit of a hack. Is there a way to force .NET/razor to render a non-localized format number?

var js_bignum = @price.ToUnlocalizedFormat();  (Is there something like this?)

I'm just trying to figure out what the best practices are for this type of situation.

Thanks!

like image 956
rocketmonkeys Avatar asked Nov 22 '11 16:11

rocketmonkeys


2 Answers

I would use @price.ToString(CultureInfo.InvariantCulture)

Edit

I have to disagree with João answer and would consider it a Bad Idea™. I would not recommend re-inventing the wheel. If you would prefer a more comprehensive approach, then I would opt to include a dedicated C# JSON encoding library (I would recommend JsonFx.NET).

The most simple approach, I would make an HtmlHelper extension. Something like:

public static class JsonExtensions {
     public static string ToJson(this HtmlHelper html) {
         return new JsonWriter().Write(t);
     }
}

Then use it in a view with:

@Html.ToJson(price)

This should handle primitive types as well as complex types.

After that I would probably refactor things and try to come up with a better injection pattern so that I'm not creating a new JsonWriter with each call to the helper.

But this will be much more flexible (and tested) than rolling your own serializer in the form of a static helper class...

like image 65
J. Holmes Avatar answered Oct 27 '22 19:10

J. Holmes


As already stated in another answer an easy solution would be to call ToString with the InvariantCulture, however this does not clearly convey your intent and may induce people to assume that InvariantCulture works for other data types.

If I were you I would wrap the formatting logic in a helper class, something like the following and call the helper methods where needed:

public static class JsLiteral
{
    public static string From(float number)
    {
        return From((double)number);
    }

    public static string From(double number)
    {
        return number.ToString(CultureInfo.InvariantCulture);
    }

    public static string From(bool flag)
    {
        return flag ? "true" : "false";
    }
}

This is easier to maintain and also clearly express your intents when you use it in a view @JsLiteral.From(bignum).

like image 31
João Angelo Avatar answered Oct 27 '22 21:10

João Angelo