开发者

C# convert any format string to double

I tried searching google and stackoverflow without success.

I'm having a problem with "Input string was not in a correct format." exception with an application I'm working at.

Thing is, that I convert some double values to strings with doubleNumber.ToString("N2"); in order to store them in XML file. When I switch testing machines, XML file stored on one can't be returned back to double values.

I've tried all of the solutions I could think of, but setting number culture didn't work, using CultureInfo.InvariantCulture, replacing characters also doesn't work. Sometimes the values are stored like "3开发者_Go百科,001,435.57" and sometimes (on other PC) like "3.001.435,57".

Is there some function or a way to parse a double from string, whatever the input format is?

Thanks.


You have to specify a culture because (eg.) "3,001" is ambiguous - is it 3.001 or 3001?

Depending on what your numbers look like, perhaps you could attempt to detect the culture by counting number of , and . characters and/or checking their positions.


Here is what you are looking for...

http://msdn.microsoft.com/en-us/library/9s9ak971.aspx

This will accept a string variable and a format provider. You need to create a format provider that provides the culture information you are looking to convert out of.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜