Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I have float number in string. there is one problem. Number uses "." not "," as decimal point.

This code is not working:

MyNumber = float.Parse("123.5");

I know that I can use string replace function to "repair" this string before Parsing.

MyNumber = float.Parse("123.5".Replace('.',',');

But is there any other way to do it?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
431 views
Welcome To Ask or Share your Answers For Others

1 Answer

Using string replace is very fragile, and will lead to suttle bugs. Specify the IFormatProvider instead. For instance:

MyNumber = float.Parse("123.5", CultureInfo.InvariantCulture);

Or you can specify the NumberFormatInfo using another overload of Parse.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...