System.Convert.ToSingle()问题,(1.5)vs(1,5) [英] System.Convert.ToSingle() Problems, (1.5) vs (1,5)

查看:793
本文介绍了System.Convert.ToSingle()问题,(1.5)vs(1,5)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在用C#编写程序来在模型格式之间进行转换。
模型格式的数字就像-0.136222这样的文本。

我可以使用System.Convert.ToSingle()将其转换为浮点数数。但在德国,我们使用逗号作为小数点(-0,136222),而System.Convert就是这样。现在我有问题,它不会识别小数点,因为它需要逗号。



简而言之,
我们有这个:-0.136222
我们得到这个:-0136222.0f
因为它期望:-0,136222

我可以告诉系统只为我的程序识别逗号作为小数点吗?解决方案

使用Single.Parse()方法可以使用Single.Parse()而是像这样:
$ b $ pre $ Single.Parse( - 0.136222,CultureInfo.InvariantCulture); InvariantCulture是告诉方法解析字符串忽略特定于语言环境的小数点和分组分隔符的方法。

I'm writing program in C# for converting between model formats. The model format has numbers as text such as "-0.136222".

I can use System.Convert.ToSingle() to convert this to a floating point number. But here in Germany we use commas as decimal points (-0,136222), and System.Convert picks up on this. Now I have the problem of it not recognizing decimal points since it expects commas.

In a nutshell; We have this: "-0.136222" We get this: -0136222.0f because it expects this: "-0,136222"

Can I tell system to recognize commas as decimal points just for my program? A work around won't work since it needs to be portable (to other countries).

解决方案

Use Single.Parse() instead, like this:

Single.Parse("-0.136222", CultureInfo.InvariantCulture);

InvariantCulture is the way to tell the method to parse the string ignoring locale-specific decimal and grouping separators.

这篇关于System.Convert.ToSingle()问题,(1.5)vs(1,5)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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