ASP.NET - 转换本地化数字
我有一个接受用户输入的文本框; 我正在尝试使用此用户输入来填充我的业务对象之一的此成员:
public System.Decimal? ExchangeRate
应用程序已本地化 - 我需要同时支持接受这些作为有效输入的文化:“1,5”和“1.5”
我现在的代码是:
var culture = Thread.CurrentThread.CurrentUICulture; int exchangeRate; int.TryParse(txtExchangeRate.Text, NumberStyles.Number, culture, out exchangeRate); entity.ExchangeRate = exchangeRate;
当用户区域性设置为需要“1,5”格式(逗号作为小数分隔符)的区域性时 - 例如“ro-RO”,我想要存储在entity.ExchangeRate中的值为1.5; 然而,当运行上面的代码时,它会被转换为 15。
关于如何转换这些不同的格式以便存储在我的业务实体中的数据为“1.5”(点作为小数分隔符)有什么建议吗?
谢谢。
你们是对的 - 使用 Thread.CurrentThread.CurrentCulture 代替 Thread.CurrentThread.CurrentUICulture 和decimal.TryParse 代替 int.TryParse 是有意义的。
但这些改变仍然不能解决我的问题。 在进一步研究代码之后,我现在可以将问题简化为:
我正在使用 telerik RadNumericTextBox 控件,该控件强制用户根据其文化使用正确的格式。 因此,当Thread.CurrentThread.CurrentCulture设置为“ro-RO”时,它只会接受“1,5”格式,而当它设置为“en-GB”时,它只会接受“1.5”格式。
这是我现在使用的代码:
decimal exchangeRate; decimal.TryParse(txtExchangeRate.Text, out exchangeRate); entity.ExchangeRate = exchangeRate;
情况 1:当前区域性是“en-GB” - 接受的输入是“1.5”,exchangeRate 设置为 1.5 - 一切正常。
情况 2:当前区域性是 "ro-RO" - 接受的输入是 "1,5" ,但在执行decimal.TryParse...行后,exchangeRate 设置为 15 - 显然是错误的。 我还应该提到,在这种情况下,txtExchangeRate.Text 的值在我的“监视”窗口中也显示为“1.5”。
所以,它看起来像decimal.TryParse会考虑当前的文化,但我找不到一种方法来真正使它适合我。 有什么建议么?
I have a textbox accepting user input; I am trying to use this user input it to populate this member of one of my business objects:
public System.Decimal? ExchangeRate
The application is localized - I need to support at the same time cultures that accept these as valid inputs: "1,5" and "1.5"
The code I have now is:
var culture = Thread.CurrentThread.CurrentUICulture; int exchangeRate; int.TryParse(txtExchangeRate.Text, NumberStyles.Number, culture, out exchangeRate); entity.ExchangeRate = exchangeRate;
When the user culture is set to a culture that expects the "1,5" format (comma as decimal separator) - e.g "ro-RO", I want the value that gets stored in entity.ExchangeRate to be 1.5; however, when running the code above, it gets converted to 15 instead.
Any suggestions on how to convert these various formats so that the data that gets stored in my business entity is "1.5" (point as decimal separator)?
Thanks.
You guys were right - it made sense to use Thread.CurrentThread.CurrentCulture instead of Thread.CurrentThread.CurrentUICulture and decimal.TryParse instead of int.TryParse.
But these changes would still not solve my problem. And after playing around with the code some more, I can now simplify the issue to this:
I am using a telerik RadNumericTextBox control which enforce users to use the correct format based on their culture. So, when Thread.CurrentThread.CurrentCulture is set to "ro-RO", it will only accept the "1,5" format, and when it's set to "en-GB", it will only accept the "1.5" format.
Here's the code I am using now:
decimal exchangeRate; decimal.TryParse(txtExchangeRate.Text, out exchangeRate); entity.ExchangeRate = exchangeRate;
Case 1: current culture is "en-GB" - accepted input is "1.5" , exchangeRate is set to 1.5 - everything works fine.
Case 2: current culture is "ro-RO" - accepted input is "1,5" , but after executing the decimal.TryParse... line, exchangeRate is set to 15 - wrong, obviously.
I should also mention that in this case, the value of txtExchangeRate.Text is also shown as "1.5" in my Watch window.
So, it looks like decimal.TryParse will take into consideration the current culture, but I can't find a way to actually make it work properly for me. Any suggestions?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
显然,int 不能容纳 1.5 ! :-) 使用 float 代替。
使用 CurrentCulture 而不是 CurrentUICulture。 我的文化是 fr-BE(因此接受 1,5,但我的 Windows UI 是英语,但不接受)。
我会使用 CurrentCulture 和 InvariantCulture 进行 float.Parse() 测试:当一些程序学会接受“1,5”时,每个人都习惯输入“1.5”。 没有什么比 Excel 在我说 1.5 时要求我输入 1,5 更让我烦恼的了! 此外,在比利时,政府推出基于网络的纳税申报的第一年,该网站强制您使用逗号而不是句号作为小数点。 每个人都想知道为什么输入的数字被拒绝!
所以善待你的用户并接受两者。
Obviously, int cannot hold 1.5 ! :-) Use float instead.
Use CurrentCulture instead of CurrentUICulture. My culture is fr-BE (therefore accepts 1,5 but my Windows UI is English, which doesn't).
I would make the float.Parse() test with both CurrentCulture AND InvariantCulture: By the time some programs learned to accept "1,5", everybody was used to type "1.5". There's nothing which bothers me more than Excel requiring me to type 1,5 when I say 1.5 ! Also, here in Belgium, the 1st year government launched the web-based tax declaration, the site forced you to use commas instead of periods as decimal points. Everybody was wondering why the figures entered were refused!
So be nice to your users and accept both.
仅供参考,我知道这不是你的问题,但它为其他可能这样做的人提供了一个指针:
当你设置你的文化时,你不能让你的应用程序能够处理不同文化的输入。 它必须是您指定的类型。
因此,如果您将 ro-RO 设置为文化,它不会将 1.5 和 1,5 理解为同一事物。
FYI I know this isn't your problem, but its a pointer for other people who might be doing this:
When you set your culture, you can't have your application be able to handle input of different cultures. It must be of the type that you have specified.
Therefore, if you set ro-RO as the culture, it won't understand both 1.5 and 1,5 as the same thing.
您可能应该使用 CurrentCulture(而不是 CurrentUICulture)进行本地化(例如日期/数字格式)。
You should probably be using CurrentCulture (as opposed to CurrentUICulture) for localization (e.g. date/number formatting).
好的,这里的代码似乎适用于我在上面的帖子中描述的两种情况(1.文化“ro-RO”,逗号作为小数分隔符和2.文化“en-GB”,点作为小数分隔符):
OK, here's the code that seems to work on both cases I described in my above post (1. culture "ro-RO", comma as decimal separator and 2. culture "en-GB", dot as decimal separator):