Double.Parse - 国际化问题 [英] Double.Parse - Internationalization problem

查看:129
本文介绍了Double.Parse - 国际化问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是推动我疯了。我有一个ASP.NET 2.0 Web窗体页

This is driving me crazy. I have the following string in a ASP.NET 2.0 WebForm Page

string s = "0.009";



够简单。现在,如果我的文化是西班牙 - 这是ES-ES - 我试图将字符串双转换,我做了以下内容:

Simple enough. Now, if my culture is Spanish - which is "es-ES" - and I try to convert the string to Double, I do the following:

double d = Double.Parse(s, new CultureInfo("es-ES"));



什么我期望是0.009。相反,我得到9.我理解.NET认为这是一个千位分隔符,这在EN-US是一个逗号,但不应该它以文化信息,我传递给parse方法和应用正确的格式换算的?

what I'd expect is 0,009. Instead, I get 9. I understand that .NET thinks it is a thousand separator, which in en-US is a comma, but shouldn't it take the culture info I'm passing to the parse method and apply the correct format to the conversion?

如果我

double d = 0.009D;
string formatted = d.ToString(new CultureInfo("es-ES"));



格式现在是0.009。任何人?

formatted is now 0,009. Anybody?

推荐答案

它的带你给文化和运用正确的格式。您提供的0.009的字符串,并告诉它,这是西班牙......那么你抱怨它正确的解读是,西班牙!不要告诉它的字符串是西班牙当你知道它是不是。

It is taking the culture you gave and applying the correct formatting. You provided a string of "0.009" and told it that it was Spanish...then you complain that it properly interpreted it as Spanish! Don't tell it that the string is Spanish when you know it isn't.

您应该通过解析方法的字符串的文化被解析,在此情况是EN-美国或EN-GB或InvariantCulture的。

You should pass the Parse method the culture of the string being parsed, which in this case would be en-US or en-Gb or InvariantCulture.

这篇关于Double.Parse - 国际化问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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