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
- 使用计算机安装的当前文化设置
- 您让用户决定在您的程序中设置他的文化 -> 用户设置
- 您必须做出决定:您必须定义并记录您的决定
- 猜测:你尝试解析,然后尝试解析,然后尝试……直到你得到有效的数字
这篇关于将字符串转换为小数分隔符“."的最佳方法和“,"麻木不仁的方式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:将字符串转换为小数分隔符“."的最佳方法和“,"麻木不仁的方式?
基础教程推荐
- 如何动态获取文本框中datagridview列的总和 2022-01-01
- 将事件 TextChanged 分配给表单中的所有文本框 2022-01-01
- JSON.NET 中基于属性的类型解析 2022-01-01
- 经典 Asp 中的 ResolveUrl/Url.Content 等效项 2022-01-01
- 全局 ASAX - 获取服务器名称 2022-01-01
- 在 VS2010 中的 Post Build 事件中将 bin 文件复制到物 2022-01-01
- 首先创建代码,多对多,关联表中的附加字段 2022-01-01
- 错误“此流不支持搜索操作"在 C# 中 2022-01-01
- 是否可以在 asp classic 和 asp.net 之间共享会话状态 2022-01-01
- 从 VS 2017 .NET Core 项目的发布目录中排除文件 2022-01-01
