C#将字符串转换为语言环境中的double [英] C# convert string to double in locale

查看:102
本文介绍了C#将字符串转换为语言环境中的double的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的语言环境中,小数点分隔符是','。

In my locale the decimal separator is a ','.

不过,我仍然想编写一个使用#的数字的C#应用​​程序。

However I would still like to write a C# application which works with numbers that use the '.' as decimal separator.

        string b = "0,5";
        double db = double.Parse(b); // gives 0.5

        string a = "0.5";
        double da = double.Parse(a); // gives 5, however i would like to get 0.5


推荐答案

您需要将区域性指定为 double.Parse ,例如

You need to specify the culture as the second argument to double.Parse, e.g.

double da = double.Parse(a, CultureInfo.InvariantCulture);

几乎所有的格式化/解析方法都带有重载,使用 IFormatProvider ,并且是最常用的 IFormatProvider 的实现是 CultureInfo

Pretty much all of the formatting/parsing methods have overloads taking an IFormatProvider, and the most commonly-specified implementation of IFormatProvider is CultureInfo.

这篇关于C#将字符串转换为语言环境中的double的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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