如何将带小数点的字符串解析为双精度值? [英] How do I parse a string with a decimal point to a double?

查看:15
本文介绍了如何将带小数点的字符串解析为双精度值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将像 "3.5" 这样的字符串解析为双精度值.然而,

I want to parse a string like "3.5" to a double. However,

double.Parse("3.5") 

产量为 35 和

double.Parse("3.5", System.Globalization.NumberStyles.AllowDecimalPoint) 

抛出一个FormatException.

现在我的计算机的语言环境设置为德语,其中逗号用作小数点分隔符.它可能必须对此做些什么,并且 double.Parse() 期望 "3,5" 作为输入,但我不确定.

Now my computer's locale is set to German, wherein a comma is used as decimal separator. It might have to do something with that and double.Parse() expecting "3,5" as input, but I'm not sure.

如何解析包含十进制数字的字符串,该字符串可能会或可能不会按照我当前语言环境中的指定进行格式化?

How can I parse a string containing a decimal number that may or may not be formatted as specified in my current locale?

推荐答案

double.Parse("3.5", CultureInfo.InvariantCulture)

这篇关于如何将带小数点的字符串解析为双精度值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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