开发者

c# Double.TryParse("1,1,1", out value) returns true, value = 111 huh?

in the immed window:

?Double.Parse("4,67,34,34,46,34,235,346")
4673434463开发者_开发知识库4235344.0

How to I stop this and only allow 4.56 4,455,455.33, 1.23E5 to succeed?


There is an overload Double.Parse(String, NumberStyles) that allow you to specify what is allowed. See this msdn page for more information.


TryParse accepts culture-specific group separators anywhere in the input string. The accepted format is

[ws][sign][integral-digits,]integral-digits[.[fractional-digits]][e[sign]exponential-digits][ws]

Where , is the culture-specific group separator and . is the decimal point.

With my current locale setting the following

double d;
Console.WriteLine(double.TryParse("1.1.1,2", out d)); // one decimal point, two group separators
Console.WriteLine(d);

evaluates to true and 111,2 while

double d;
Console.WriteLine(double.TryParse("1.1.1,2,2", out d)); // two decimal points
Console.WriteLine(d);

evaluates to false and 0.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜