开发者

Specify double.TryParse(..) number format?

开发者 https://www.devze.com 2023-02-02 00:23 出处:网络
I get lat/lon strings from an API back in the format e.g. \'37.766713\' or \'-122.428938\' (see http://en.wikipedia.org/wiki/Decimal_degrees) and want to parse these into a double each, however, doubl

I get lat/lon strings from an API back in the format e.g. '37.766713' or '-122.428938' (see http://en.wikipedia.org/wiki/Decimal_degrees) and want to parse these into a double each, however, double.TryParse(string, out doubleInstance) gives me a double of '37766713,0'. Is there any elegant way to specify the input format for the parsing? Doing string.Repl开发者_如何学运维ace(".", ",") hurts a little bit.


This is a culture issue. Try using the InvariantCulture.

double d = Double.Parse("37.766713", CultureInfo.InvariantCulture);


Sigh.. should have looked a little longer.. this does the trick: Double.TryParse() input decimal separator different than system decimal separator


Take a look of the Convert.ToDouble() or Double.parse(), you can specify a IFormartProvider for the specific number format.

0

精彩评论

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