"将字符串转换为十进制分隔符&QUOT的最佳方式;和","不敏感的方法吗? [英] Best way to convert string to decimal separator "." and "," insensitive way?

查看:561
本文介绍了"将字符串转换为十进制分隔符&QUOT的最佳方式;和","不敏感的方法吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

应用程序处理与再present小数是来自不同文化背景的字符串。 例如1.1和1,1是相同的值。

我打了 Decimal.TryParse 标记的组合,但不能达到我想要的结果。毕竟1,1变成了11或0。

是否有可能转换这样的字符串转换为十进制的一行code,而不pre-替换,字符为。或 NumberFormat.NumberDecimalSeparator

你是如何处理这种情况?

感谢你在前进!

解决方案

您有以下possibllities:

  1. 您知道文化
    1. 使用当前的文化环境,是安装了计算机
    2. 您让用户决定设置自己的文化 - 在你的程序>用户设置
  2. 您不知道文化
    1. 您必须决定这件事:你必须定义并记录你的决定
    2. 猜:你尝试解析,并试图解析,并尝试......直到你得到有效的号码

Application deals with strings that represent decimals that come from different cultures. For example "1.1 and "1,1" is the same value.

I played with Decimal.TryParse flags combinations but couldn't achieve the result I want. "1,1" became "11" or "0" after all.

Is it possible to convert such strings to decimal in one line of code without pre-replacing "," char to "." or playing with NumberFormat.NumberDecimalSeparator ?

How do you handle such situations?

Thank you in advance!

解决方案

You have the following possibllities:

  1. You know the culture

    1. Use the current Culture setting, for which the computer is installed
    2. You let the user decide to set his culture -> user settings in your program

  2. You do not know the culture

    1. You must decide about it: you have to define and document your decision
    2. Guess: you try to parse, and try to parse, and try to ... until you get valid numbers

这篇关于"将字符串转换为十进制分隔符&QUOT的最佳方式;和","不敏感的方法吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆