简体   繁体   English

输入正确的Double.Parse(“ string”)FormatException

[英]Double.Parse(“string”) FormatException with correct input

I'm trying to parse 我正在尝试解析

Double.Parse(rowData[j++]).

The input sting is correct ("7,412"), j=123 so it worked until this point. 输入的字符串正确(“ 7,412”),j = 123,因此一直有效。 All numbers are written with ",". 所有数字均以“,”书写。

Even more if I type Double.Parse(rowData[j]) in watch it works. 如果我在手表中键入Double.Parse(rowData[j]) ,它的功能Double.Parse(rowData[j])更多。

What is the problem here? 这里有什么问题?

You have comma instead of your decimal separator. 您用逗号代替小数点分隔符。

http://msdn.microsoft.com/en-us/library/3s27fasw http://msdn.microsoft.com/en-us/library/3s27fasw

You should use AllowThousands NumberType if that's thousands separator 如果是千位分隔符,则应使用AllowThousands NumberType

NumberStyles styles = NumberStyles.AllowThousands | NumberStyles.Float;
Double.Parse(value, styles);

Or you should replace your comma with . 或者,您应将逗号替换为. using string.Replace prior to parsing if that's considered decimal separator 如果认为是十进制分隔符, string.Replace在分析之前使用string.Replace

According to MSDN you can try: 根据MSDN,您可以尝试:

value = "4,320.64";
styles = NumberStyles.AllowThousands |
            NumberStyles.Float; 
number = Double.Parse(value, styles);

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

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