C#解析浮子字符串 [英] C# parsing float from string

查看:170
本文介绍了C#解析浮子字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从XML文件中读取数字。其他的数字是用逗号分隔符(0,1111)等人用点(0.1111)。我如何分析这些数字,所以我得到最终想要的结果?我试着用 float.Parse(reader.Value,System.Globalization.CultureInfo.InvariantCulture); ,但它不工作。比如我有reader.Value =0,01119703,并解析为1119703.0。

I'm reading numbers from XML files. Other numbers are with a comma separator (0,1111) and others with dot (0.1111). How do I parse these numbers so I get the desired result in the end? I tried using float.Parse(reader.Value, System.Globalization.CultureInfo.InvariantCulture); but it doesn't work. For example I have reader.Value = "0,01119703" and is parsed as 1119703.0.

推荐答案

我不相信有可能在同一时间用两个不同的小数分隔工作。我想,我只想用替换()来更改任何逗号进入点。

I don't believe that it is possible to work with two different decimal separators at the same time. I think I would just use Replace() to change any commas into dots.

float.Parse(reader.Value.Replace(',', '.'), System.Globalization.CultureInfo.InvariantCulture);

这篇关于C#解析浮子字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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