问题解析货币文本小数类型 [英] Problem parsing currency text to decimal type

查看:95
本文介绍了问题解析货币文本小数类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图像45.59 $字符串解析为一个小数。出于某种原因,我得到的异常,输入不正确的格式。因为这不会是一个全球性的计划,我不关心所有的本地化东西。下面是我在做什么。你看到什么问题?

 的NumberFormatInfo MyNFI =新的NumberFormatInfo();
MyNFI.NegativeSign = - ;
MyNFI.NumberDecimalSeparator =;
MyNFI.NumberGroupSeparator =,;
MyNFI.CurrencySymbol =$;
十进制D = decimal.Parse($ 45.00,MyNFI); //此处抛出异常...


解决方案

如何使用:

 十进制D = decimal.Parse($ 45.00,NumberStyles.Currency);

Decimal.Parse MSDN文档:


  

s参数是使用NumberStyles.Number风格PTED间$ P $,这意味着空白和千位分隔符是允许的,但货币符号都没有。要明确定义的元素(如货币符号,千位分隔符,和空格),可以以s present,使用Decimal.Parse(字符串,的NumberStyles,的IFormatProvider)方法


I am trying to parse a string like "$45.59" into a decimal. For some reason I am getting exception that the input was not in the correct format. I don't care about all the localization stuff because this is not going to be a global program. Here is what I am doing. Do you see any problems?

NumberFormatInfo MyNFI = new NumberFormatInfo(); 
MyNFI.NegativeSign = "-"; 
MyNFI.NumberDecimalSeparator = "."; 
MyNFI.NumberGroupSeparator = ",";
MyNFI.CurrencySymbol = "$"; 
decimal d  = decimal.Parse("$45.00", MyNFI);    // throws exception here...

解决方案

How about using:

decimal d = decimal.Parse("$45.00", NumberStyles.Currency);

The MSDN documentation on Decimal.Parse states:

"The s parameter is interpreted using the NumberStyles.Number style. This means that white space and thousands separators are allowed but currency symbols are not. To explicitly define the elements (such as currency symbols, thousands separators, and white space) that can be present in s, use the Decimal.Parse(String, NumberStyles, IFormatProvider) method

这篇关于问题解析货币文本小数类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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