C# 字符串到十进制转换:点分隔而不是逗号
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19893407/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
String to decimal conversion: dot separation instead of comma
提问by LoveMyWife
I have a string read from a textbox. It contains a comma for decimal separation.
我有一个从文本框中读取的字符串。它包含用于十进制分隔的逗号。
I have NumberFormatInfo.CurrencyDecimalSeparator
set to ,
(comma) but when I convert the string to decimal Convert.ToDecimal(mystring);
I obtain a dot separate value for decimal.
我已NumberFormatInfo.CurrencyDecimalSeparator
设置为,
(逗号),但是当我将字符串转换为十进制时,Convert.ToDecimal(mystring);
我获得了十进制的点分隔值。
Example:
例子:
decimal a=Convert.ToDecimal("1,2345"); ----> decimal is 1.2345
I have tried also:
我也试过:
double a=Convert.ToDouble("1,2345");
but dot for decimal again
但再次点为十进制
采纳答案by LoveMyWife
Thanks for all reply.
谢谢大家的回复。
Because I have to write a decimal number in a xml file I have find out the problem. In thisdiscussion I have learned that xml file standard use dot for decimal value and this is culture independent.
So my solution is write dot decimal number in a xml file and convert the readed string from the same xml file mystring.Replace(".", ",");
Thanks Agatfor suggestion to research the problem in xml context and Ε Г И ? И Оbecause I didn't know visual studio doesn't respect the culture settings I have in my code.
因为我必须在 xml 文件中写一个十进制数,所以我找到了问题所在。在本次讨论中,我了解到 xml 文件标准使用点表示十进制值,这与文化无关。所以我的解决方案是在 xml 文件中写入点十进制数并从同一个 xml 文件中转换读取的字符串mystring.Replace(".", ",");
感谢Agat建议在 xml 上下文中研究问题和Ε Г И ? И О因为我不知道 Visual Studio 不尊重我在代码中的文化设置。
回答by Agat
All this is about cultures. If you have any other culture than "US English" (and also as good manners of development), you should use something like this:
所有这些都与文化有关。如果您有“美国英语”以外的任何其他文化(以及良好的发展方式),您应该使用以下内容:
var d = Convert.ToDecimal("1.2345", new CultureInfo("en-US"));
// (or 1,2345 with your local culture, for instance)
(obviously, you should replace the "en-US" with the culture of your number local culture)
(显然,您应该将“en-US”替换为您的号码本地文化的文化)
the same way, if you want to do ToString()
同样的方式,如果你想做 ToString()
d.ToString(new CultureInfo("en-US"));
回答by Ε Г И ? И О
You are viewing your decimal or double values in Visual Studio. That doesn't respect the culture settings you have on your code.
您正在 Visual Studio 中查看十进制或双精度值。这不尊重您在代码中的文化设置。
Change the Region and Language settings on your Control Panel if you want to see decimal
and double
values having the comma as the decimal separator.
如果你想看到更改控制面板上的区域和语言设置decimal
,并double
具有逗号作为小数点分隔符值。
回答by Ashok Mandial
Instead of replace we can force culture like
我们可以强迫文化,而不是替换
var x = decimal.Parse("18,285", new NumberFormatInfo() { NumberDecimalSeparator = "," });
it will give output 18.285
它将给出输出 18.285
回答by Gervo
I ended up using this solution.
我最终使用了这个解决方案。
decimal weeklyWage;
decimal.TryParse(items[2],NumberStyles.Any, new NumberFormatInfo() { NumberDecimalSeparator = "."}, out weeklyWage);
回答by Varma Naidu V
I had faced the similar issue while using Convert.ToSingle(my_value) If the OS language settings is English 2.5 (example) will be taken as 2.5 If the OS language is German, 2.5 will be treated as 2,5 which is 25 I used the invariantculture IFormat provided and it works. It always treats '.' as '.' instead of ',' irrespective of the system language.
我在使用 Convert.ToSingle(my_value) 时遇到了类似的问题 如果操作系统语言设置为英语 2.5(示例)将被视为 2.5 如果操作系统语言是德语,2.5 将被视为 2,5,即我使用的 25提供的不变文化 IFormat 并且它有效。它总是对待 '.' 作为 '。' 而不是 ',' 与系统语言无关。
float var = Convert.ToSingle(my_value, System.Globalization.CultureInfo.InvariantCulture);
float var = Convert.ToSingle(my_value, System.Globalization.CultureInfo.InvariantCulture);
回答by Nikolay Nechay
usCulture = new CultureInfo("vi-VN");
Thread.CurrentThread.CurrentCulture = usCulture;
Thread.CurrentThread.CurrentUICulture = usCulture;
usCulture = Thread.CurrentThread.CurrentCulture;
dbNumberFormat = usCulture.NumberFormat;
number = decimal.Parse("1.332,23", dbNumberFormat); //123.456.789,00
usCulture = new CultureInfo("en-GB");
Thread.CurrentThread.CurrentCulture = usCulture;
Thread.CurrentThread.CurrentUICulture = usCulture;
usCulture = Thread.CurrentThread.CurrentCulture;
dbNumberFormat = usCulture.NumberFormat;
number = decimal.Parse("1,332.23", dbNumberFormat); //123.456.789,00
/*Decision*/
var usCulture = Thread.CurrentThread.CurrentCulture;
var dbNumberFormat = usCulture.NumberFormat;
decimal number;
decimal.TryParse("1,332.23", dbNumberFormat, out number); //123.456.789,00