Convert.ToDouble的本地化问题 [英] Localization issue with Convert.ToDouble

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

问题描述

我的Silverlight应用程序遇到本地化问题.我正在研究一种财务应用程序,并在网格上显示公司报告的数据.我正在尝试使用en-US(英语-美国)作为默认本地化来实现本地化工作,但是我想将本地化更改为es-AR(西班牙文-阿根廷),并且我面临以下问题.

我的值是0.21

在en-US中,它表示为0.21;在es-AR中,它表示为0,21

当我的计算机本地化为es-AR Convert.ToDouble("0.21")时,其转换为21.0

我在互联网上进行搜索,发现我需要通过文化编号格式,但仍然存在相同的问题

  double  dblValue = Convert.ToDouble(" ); 


//输出为21.00

 Thread.CurrentThread.CurrentCulture =  CultureInfo("  es-AR");
Thread.CurrentThread.CurrentUICulture =  CultureInfo(" );

numFormatInfo.NumberDecimalSeparator = " ;
numFormatInfo.NumberGroupSeparator = " ;

 double  dblValue = Convert.ToDouble("  span>,numFormatInfo); 


//出局再次相同21.00

另外,以下语句在"es-AR"文化中引发无效数字格式的错误

  double  dblValue = Convert.ToDouble(" );
 double  dblValue = Convert.ToDouble("  span>,numFormatInfo); 


//引发错误-

 System.FormatException:输入字符串不是 <使用正确的格式span class ="code-keyword">.
zh_cn System.Number.ParseDouble(字符串值,NumberStyles选项,NumberFormatInfo numfmt)
zh-CN System.Convert.ToDouble(字符串值)




那么我应该如何实现呢?

解决方案

尝试一下:

 CultureInfo myUSCulture =  CultureInfo(" );
                CultureInfo myUKCulture =  CultureInfo(" );
                CultureInfo myZACulture =  CultureInfo(" );
                 Double  waarde = Convert.ToDouble(textBox2.Text,Thread.CurrentThread.CurrentCulture.NumberFormat);

                MessageBox.Show(waarde.ToString(" ,myUSCulture));
                MessageBox.Show(waarde.ToString(" ,myUKCulture));
                MessageBox.Show(waarde.ToString(" ,myZACulture));
                MessageBox.Show(waarde.ToString(" ,Thread.CurrentThread.CurrentCulture)) pre> 

您需要特定区域性而不是线程中的区域性


您所描述的行为是正确的,并且您应该使用任何UI控件来遵守本地数字格式设置.只需使用Double.Parse和Double.ToString(或Decimal或您使用的任何数字类型)就可以很好地进行本地交互.

仅当要将数字与需要在所有语言环境(例如文件)上使用的规范字符串格式进行转换时,才需要指定区域性.在这种情况下,通常应使用CultureInfo.InvariantCulture进行格式化.在Silverlight应用程序中,这并不是您需要做的事情(但是,如果您使用WCF服务与主机Web应用程序进行通信,则所有字符串转换都将为您处理,而您不必在意)


.设置线程区域性时,将根据该区域性的格式对数字进行格式化和读取(至少在使用double.ToString("N0")和double.Parse(或double时)中. .TryParse).

在Web应用程序上,如果从资源中加载本地化的字符串或使用数字,通常会在每个请求之前设置线程区域性.

如果使用预定义的区域性,则不必修改numFormatInfo,并且如果要使用与线程区域性不同的区域性,则可以传递CultureInfo对象.

如另一种解决方案中所述,通常需要在文件中保存值(例如使用Open XML SDK的Excel文件)时使用InvariantCulture.


I am facing localization issue with my Silverlight application. I am working on one financial application and showing the company reported data on grid. I am trying to implement the localization stuffs, having en-US (English - United States) as default localization, but i want to change the localization to es-AR (Spanish - Argentina) and i am facing the following issue.

My value is 0.21

In en-US it is represented as 0.21 and In es-AR it is represented as 0,21

When my machine localization is es-AR Convert.ToDouble("0.21") is getting converted as 21.0

I have searched on internet and found that i need to pass the culture number format, still having the same issue

double dblValue = Convert.ToDouble("0.21");


// Out Put is 21.00

Thread.CurrentThread.CurrentCulture = new CultureInfo("es-AR");
Thread.CurrentThread.CurrentUICulture = new CultureInfo("es-AR");

numFormatInfo.NumberDecimalSeparator = ",";
numFormatInfo.NumberGroupSeparator = ".";

double dblValue = Convert.ToDouble("0.21", numFormatInfo);


// Out Put is same again 21.00

Also the following statements throws me error of invalid number format on "es-AR" culture

double dblValue = Convert.ToDouble("86,131,789");
double dblValue = Convert.ToDouble("86,131,789", numFormatInfo);


// Throws Error -

System.FormatException: Input string was not in the correct format.
en System.Number.ParseDouble(String value, NumberStyles options, NumberFormatInfo numfmt)
en System.Convert.ToDouble(String value)




So how should i achieve this ?

解决方案

try this:

CultureInfo myUSCulture = new CultureInfo("en-US");
                CultureInfo myUKCulture = new CultureInfo("en-GB");
                CultureInfo myZACulture = new CultureInfo("af-ZA");
                Double waarde = Convert.ToDouble(textBox2.Text, Thread.CurrentThread.CurrentCulture.NumberFormat);

                MessageBox.Show(waarde.ToString("C", myUSCulture));
                MessageBox.Show(waarde.ToString("C", myUKCulture));
                MessageBox.Show(waarde.ToString("C", myZACulture));
                MessageBox.Show(waarde.ToString("C", Thread.CurrentThread.CurrentCulture));



you want specific culture and not culture from thread


The behaviour you described is correct, and you should respect the local number format settings with any UI controls. Just using Double.Parse and Double.ToString (or Decimal or whatever number type you are using) will work fine for local interactions.

You should only need to specify a culture if you are transforming numbers to or from a canonical string format that needs to work on all locales (for example files). In that case you should typically use CultureInfo.InvariantCulture to do the formatting. In a Silverlight app this is not something that you should need to do much, though (if you use a WCF service to communicate with the host web app, all the string transformations are handled for you and you don''t have to care).


When you set thread cultures, numbers are formatted and read according to the rule for that culture (at least when using double.ToString("N0") and double.Parse (or double.TryParse).

On a web application, you will typically set the thread culture before each request if you load localized string from resources or works with numbers.

If you uses predefined culture, you don''t have to modify numFormatInfo and you can pass the CultureInfo object if you want to use a culture that if different than the thread culture.

As mentionned in another solution, you would typically uses the InvariantCulture when you need to save a value in a file (like an Excel file using Open XML SDK).


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

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