转换为浮点数时的C#问题 [英] C# problem when converting to float

查看:120
本文介绍了转换为浮点数时的C#问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好!

我想转换为浮点数.我知道这是很简单的事情,但是很难找到解决这个问题的方法...
所以这是代码:

Hello!

I wanted to convert to float. I know, it is very simple thing to do, but it''s harder to find a solution to this problem...
So heres the code:

string s = "1.1"
float f = float.Parse(s);




现在我得到一个formatexception.我不知道为什么?!




Now I get a formatexception. I can''t figure out why?!

推荐答案

最可能出现此错误的原因是您的区域设置对使用了不同的字符. (小数点)

试试:

The most likely reason for your error is that your regional settings use a different character for . (decimal point)

Try:

float.Parse(s, CultureInfo.InvariantCulture);


此代码如果添加缺少的;",则按预期工作第一行之后-mdash;在默认区域性中.
您的线程文化是什么?也许,"应作为分数定界符(小数点)—因为文化不同?
This code works as you expect if you add missing ";" after first line — in default culture.
What is your thread culture? Maybe "," is expected as fraction delimiter (decimal point) — due to different culture?


这篇关于转换为浮点数时的C#问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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