简体   繁体   English

试图将字符串转换为双C#?

[英]Trying to convert string into double C#?

 double test = Convert.ToDouble("39,618840‎");

这给了我格式异常,并且我尝试使用Cultureinfo.invariantculture设置,它的作用相同。

You have a trailing invisible character after the zero. 零后有一个尾随的不可见字符。 Remove it. 去掉它。

在此处输入图片说明

Then, this works: 然后,这有效:

var culture = CultureInfo.GetCultureInfo("FR-fr");       
var qty = Convert.ToDouble("39,618840", culture);

There are some invalid characters in that string. 该字符串中包含一些无效字符。

Looking at the hex string we see the following: 查看十六进制字符串,我们看到以下内容:

"39,618840‎" --> 0x22, 0x33, 0x39, 0x2c, 0x36, 0x31, 0x38, 0x34, 0x30, 0xe2, 0x80, 0x8e, 0x22

And the characters actually look like: 字符实际上看起来像:

"39,618840â€Ẑ"
        if (myString.Contains(","))
            myString = myString.Replace(",", "");
        double mydouble = Double.Parse(myString);

Using the above method will parse a string into a double, your issue is the comma. 使用上述方法会将字符串解析为双精度型,您的问题是逗号。 If you intended for the comma to act as the decimal point, the following amendment can be made: 如果您打算将逗号用作小数点,则可以进行以下修改:

        myString = myString.Replace(",", ".");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM