System.Convert.ToSingle()问题,(1.5)vs(1,5)

编程入门 行业动态 更新时间:2024-10-27 02:27:48
本文介绍了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)

本文发布于:2023-11-07 06:53:04,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1565816.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:Convert   System   ToSingle

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!