为什么 Html.DisplayFor 和 .ToString(“C2”) 不尊重 CurrentUICulture?
在我的 ASP.MVC 2.0 网站中,我在 web.config 中有以下设置:
<globalization uiCulture="da-DK" culture="en-US" />
当我尝试使用 Html.DisplayFor() 或 ToString("C2") 在视图中显示金额时,我期望得到“kr. 3.500,00 “(uiCulture)而不是“$3,500.00”(文化)。
<%:Html.DisplayFor(posting => posting.Amount)%>
<%:Model.Amount.ToString("C2")%>
如果我明确使用 CurrentUICulture 信息,它会按预期工作,但我不想每次需要显示数字、日期或小数时都这样做。而且我还喜欢使用 DisplayFor,它不支持 IFormatProvider 参数。
<%:Model.Amount.ToString("C2", System.Globalization.CultureInfo.CurrentUICulture)%>
如何在不改变系统文化的情况下更改格式?
这是在 Azure 中运行的,如果我将区域性更改为“da-DK”,则在保存到 Azure 表存储时,所有小数点都会丢失! #漏洞
In my ASP.MVC 2.0 website I have the following setting in web.config:
<globalization uiCulture="da-DK" culture="en-US" />
When I try to display an amount in a view using Html.DisplayFor() or ToString("C2") I expected to get "kr. 3.500,00" (uiCulture) and not "$3,500.00" (culture).
<%:Html.DisplayFor(posting => posting.Amount)%>
<%:Model.Amount.ToString("C2")%>
If I explicit uses CurrentUICulture info it works as expected, but I don't want to do that everytime I need to display a number, date or decimal. And I also like to use DisplayFor, which doesn't support the IFormatProvider parameter.
<%:Model.Amount.ToString("C2", System.Globalization.CultureInfo.CurrentUICulture)%>
How can I change the formatting, without changing the culture of the system?
This is running in Azure, and if I change the culture to "da-DK" all decimal points are lost, when saving to Azure Table storage! #BUG
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
UI区域性用于查找和加载资源,区域性用于格式化。
因此,不采用区域性的各种
ToString(string)
和String.Format
重载将使用线程的当前区域性 (System.Globalization.CultureInfo.CurrentCulture
) 进行格式化。如果您想对货币、日期等使用丹麦格式,则需要将 Thread.CurerentThread.CurrentCulture 设置为 CultureInfo.GetCultureInfo("da-DK") (直接或间接)。
总结:你的文化和 UI 文化是错误的。
The UI culture is used to lookup and load resources, the Culture is used for formatting.
So the various
ToString(string)
andString.Format
overloads that don't take a culture will use the thread's current Culture (System.Globalization.CultureInfo.CurrentCulture
) to format.If you want to use Danish formatting for currency, dates, ... then
Thread.CurerentThread.CurrentCulture
needs to be set toCultureInfo.GetCultureInfo("da-DK")
(directly or indirectly).Summary: you have Culture and UI Culture the wrong way around.