Question

I am trying to convert a float from one culture to another, and seems nothing i found online was working for me. The problem is:

  1. I have a parameter coming from external resource (web service) in a specific culture.
  2. the result is send to an MVC application, and VALIDATED depending on the current culture settings.

this means i have to somehow convert from the static culture ("en-US") to whatever is selected ON THE CLIENT, before the data is sent to the server for processing, so all the data from the client is on one culture.

I believed that Globalization jQuery plugin (GitHub) will provide this functionality for formatting numbers (and dates) in given culture out-of-the-box, but it seems it do not.

What i tried: using the Globalization plugin, setting Globalize.culture property, and hoping that Globalize.format(Globalize.parseFloat("1.2435677777", 10, "en-US"))) will format the value with the desired decimal separator. However it seems it does not worked for me.

If anyone can point what i did wrong, and give a better working example - will be great.

Was it helpful?

Solution

As the Globalize.format() function always returns a '.' decimal separator, as far as i understood, there is a little hacky solution, but at least it works. The point is at replacing the 'neutral' decimal separator with that for the current culture

Globalize.parseFloat("1.2435677777", 10, "en-US").toString().replace(".", Globalize.cultures[Globalize.cultureSelector].numberFormat["."]) 

OTHER TIPS

Works for me if i specify a format for the format function.

Globalize.culture("sv");
Globalize.format(Globalize.parseFloat("1.2435677777", 10, "en-US"), "N");

Gives: '1,24'

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top