字符串浮充转换 - 小数点分隔符 [英] string to float conversion - decimal separator

查看:100
本文介绍了字符串浮充转换 - 小数点分隔符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在用下面的代码有问题:

I'm having a problem with the following code:

string latString = "50.09445";
float lat = Convert.ToSingle(latString);



第二个命令抛出出现FormatException异常。我知道,问题是我使用(CS-CZ)使用逗号作为小数点分隔的区域性设置这个字符串包含小数点来代替。

The second command throws a FormatException exception. I know that problem is that culture settings I'm using (cs-CZ) use comma as decimal separator and this string contains decimal point instead.

有没有一些简单的方法以忽略文化设置,并总是用小数点转​​换?或者我应该首先检查该字符串避免这个问题,并由小数点替换逗号?

Is there some easy way to "ignore" the culture settings and always use decimal point for the conversion? Or should I just avoid the problem by checking the string first and replace comma by the decimal point?

推荐答案

使用CultureInfo.InvariantCulture

Use CultureInfo.InvariantCulture

float lat = Convert.ToSingle("50.09445", CultureInfo.InvariantCulture);

这篇关于字符串浮充转换 - 小数点分隔符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆