简体   繁体   中英

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(",", ".");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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