Best way to convert string to decimal separator quot;.quot; and quot;,quot; insensitive way?(将字符串转换为小数分隔符“.的最佳方法和“,麻木不仁的方式?)
问题描述
应用程序处理代表来自不同文化的小数的字符串.例如1.1"和1,1"是同一个值.
Application deals with strings that represent decimals that come from different cultures. For example "1.1 and "1,1" is the same value.
我使用了 Decimal.TryParse
标志组合,但无法达到我想要的结果.1,1"毕竟变成了11"或0".
I played with Decimal.TryParse
flags combinations but couldn't achieve the result I want. "1,1" became "11" or "0" after all.
是否可以在一行代码中将此类字符串转换为十进制,而无需预先将,"字符替换为."或玩 NumberFormat.NumberDecimalSeparator
?
Is it possible to convert such strings to decimal in one line of code without pre-replacing "," char to "." or playing with NumberFormat.NumberDecimalSeparator
?
你如何处理这种情况?
提前感谢您!
推荐答案
你有以下可能:
- 您了解这种文化
- You know the culture
- 使用计算机安装的当前文化设置
- 您让用户决定在您的程序中设置他的文化 -> 用户设置
- 您必须做出决定:您必须定义并记录您的决定
- 猜测:你尝试解析,然后尝试解析,然后尝试……直到你得到有效的数字
这篇关于将字符串转换为小数分隔符“."的最佳方法和“,"麻木不仁的方式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:将字符串转换为小数分隔符“."的最佳方法和“,"麻木不仁的方式?


- 带问号的 nvarchar 列结果 2022-01-01
- 在 C# 中异步处理项目队列 2022-01-01
- Azure Active Directory 与 MVC,客户端和资源标识同一 2022-01-01
- CanBeNull和ReSharper-将其用于异步任务? 2022-01-01
- Windows 喜欢在 LINUX 中使用 MONO 进行服务开发? 2022-01-01
- 使用 rss + c# 2022-01-01
- 为什么 C# 中的堆栈大小正好是 1 MB? 2022-01-01
- 是否可以在 .Net 3.5 中进行通用控件? 2022-01-01
- C# 通过连接字符串检索正确的 DbConnection 对象 2022-01-01
- 在 LINQ to SQL 中使用 contains() 2022-01-01