Reputation: 8649
I'm working on a Web application that has some ASP.NET code that generates some values in javascript on the Views.
var userLocation = { lat: @Model.Location.Latitude, lng: @Model.Location.Longitude };
This works just fine on my computer and it generates something like this:
var userLocation = { lat: 9.9333333, lng: -84.0833333 };
The problem is that I was setting the dev environment on another computer that had the locale settings set to spanish, in which case the generated javascript looks like this (note the comma as a decimal separator):
var userLocation = { lat: 9,9333333, lng: -84,0833333 };
And this will clearly show an error in javascript.
I changed the settings in the computer to use US locale settings but it isn't changing anything on the output (I rebuilt the solution and even restarted the computer but to no effect).
I was wondering what's the best way to set the locale settings in the web application so it always generates the dot as the decimal separator.
Thanks! Will
Upvotes: 2
Views: 2075
Reputation: 21898
var userLocation = {
lat: @Model.Location.Latitude.ToString(CultureInfo.InvariantCulture),
lng: @Model.Location.Longitude.ToString(CultureInfo.InvariantCulture)
};
Alternately, if you don't want to modify your views, add this line in your controller:
System.Threading.Thread.CurrentThread.CurrentCulture = CultureInfo.InvariantCulture;
Note that this may cause side effects such as on date formatting.
And if you don't want any change at all in the code, follow Fabio's advice about web.config. But by doing this, you're not doing your users a favor since: Again, date formatting et al.
Upvotes: 0
Reputation: 11990
I suppose that Latitude is a decimal or double value, right? Try this
CultureInfo usa = new CultureInfo("en-US");
var userLocation = { lat: @Model.Location.Latitude.ToString(usa), lng: @Model.Location.Longitude.ToString(usa) };
You need to define a specific CultureInfo for your variable. So no matter what the current culture, it will always be in the specified CultureInfo.
If you want to change the culture of the whole application, you need to set the globalization in the Web.Config > system.web
<globalization uiCulture="en" culture="en-US" />
Upvotes: 3