Reputation: 3742
I have a MVC site that I would like to run in Danish.
Based on this thread I got that to work.
.Net MVC4 Culture set correct, but validation still English
But I also have a few places where I would like to generate JavaScript, and here I would like the regional settings to be English, because this is the only thing valid in JavaScript.
@Model.price
Will give 9,95
string.Format("{0:#.##}",Model.price)
Also gives 9,95.
And
string.Format("{0:#,##}",Model.price)
Would give 10.
So what is the best practice to build correct "English" JavaScript using Razor when the site is in Danish?
Upvotes: 0
Views: 688
Reputation: 131443
Your problem is that String.Format without a specific culture will still use the current thread's culture for parsing and converting to string. By setting the page's Culture to Danish, you specified that Danish should be used for string conversions as well.
You can specify the culture you want to use by passing it as the first parameter to String.Format, eg:
string.Format(CultureInfo.InvariantCulture,"{0:#,##}",Model.price)
You can pass the culture in any method where an IFormatProvider parameter is expected. String.Format and almost all ToString() and Parse() methods have overloads that accept this parameter.
Tools like Resharper will even warn you for possible conversion errors whenever you use such a method without passing a specific culture
By the way, the correct culture to use is CultureInfo.InvariantCulture, not English, otherwise you may end up with US-style dates etc.
Upvotes: 1