C#类型转换问题 [英] C# type convert problem
本文介绍了C#类型转换问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
Hi.a字符串表达式转换为双倍此表达式前198.34转换后19834.0
例子;
string d =198.34;
double w = Convert.ToDouble(d); ===> w = 19834.0
Hi.a string expression convert double when this expression ago "198.34" converted after 19834.0
example;
string d="198.34";
double w =Convert.ToDouble(d); ===>w=19834.0
推荐答案
Insetead Convert.ToDouble(),使用 Double.TryParse()方法 [ ^ ]
示例:
Insetead Convert.ToDouble(), use Double.TryParse() method[^]
Example:
string d="198.34";
double w;
NumberStyles style = System.Globalization.NumberStyles.Number | System.Globalization.NumberStyles.AllowCurrencySymbol;
CultureInfo culture = CultureInfo.CreateSpecificCulture("en-GB");
Double.TryParse(d,style,culture ,out w);
Console.WriteLine(w.ToString());
Console.ReadKey();
Dim d As String = "198.34"
Dim w As Double = 0.0
Dim style As System.Globalization.NumberStyles = System.Globalization.NumberStyles.Number Or System.Globalization.NumberStyles.AllowCurrencySymbol
Dim culture As System.Globalization.CultureInfo = System.Globalization.CultureInfo.CreateSpecificCulture("en-GB")
Double.TryParse(d, style, culture, w)
Console.WriteLine(w.ToString())
Console.ReadKey()
It取决于您的区域设置,如果您正在使用。作为一个千位分隔符,那么这就是你得到的结果。您需要确保您的区域设置正在使用。作为小数点分隔符。您可以通过转到控制面板>区域和语言然后点击其他设置按钮(这是在Win7和Win8上)进行调整。
It depends on your regional settings, if you are using the . as a thousands separator then that's the result you will get. You need to make sure that your regional settings is using . as a decimal separator. You can adjust this by going to Control Panel>Region and Language then hit the "Additional Settings" button (this is on Win7 and Win8).
这篇关于C#类型转换问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文