English 中文(简体)
将货币文本解析为十进制类型时出现问题
原标题:Problem parsing currency text to decimal type

我正在尝试将类似“$45.59”的字符串解析为十进制。由于某种原因,我对输入的格式不正确感到异常。我不在乎所有本地化的东西,因为这不会是一个全球性的项目。以下是我正在做的事情。你看到什么问题了吗?

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

如何使用:

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

十进制。分析状态:

“s参数是使用NumberStyles.Number样式解释的。这意味着允许使用空格和千位分隔符,但不允许使用货币符号。要显式定义s中可以存在的元素(如货币符号、千位分隔符号和空格),请使用Decimal.Parse(String、NumberSttyles、IFormatProvider)方法

问题回答

这种方式对我来说很有效:

NumberFormatInfo MyNFI = new NumberFormatInfo();
MyNFI.NegativeSign = "-";
MyNFI.CurrencyDecimalSeparator = ".";
MyNFI.CurrencyGroupSeparator = ",";
MyNFI.CurrencySymbol = "$";

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

1.) You have to define the currency separator instead of the number separator. 2.) Because you defined the currency values only, you need to define the NumberStyles.Currency while parsing.

When I tried to run the code from @JohnKoerner, it would fail with the exception: System.FormatException, with the message: "Input string was not in a correct format.". @MEN s answer was helpful, but I wanted to add some additional insight about the accepted answer and how to fix that problem.

就像@MEN一样,在.Passe()方法正常工作之前,我必须包含NumberFormatInfo。但是,我不需要用CurrencyDecimalSeparator指定小数。您必须包含数字所需的所有属性。以下是类定义文档中的列表:

MSDN文档-NumberFormatInfo类

在我的实现中,我永远不会得到负数,所以我选择不包括它。以下是我所拥有的:

string currencyAmount = "$45.00";

NumberFormatInfo FormatInfo = new NumberFormatInfo();
FormatInfo.CurrencyGroupSeparator = ",";
FormatInfo.CurrencySymbol = "$";

// Result: 45.00
decimal parsedCurrency = decimal.Parse(currencyAmount, NumberStyles.Currency, FormatInfo);

请在窗口中检查区域设置。如果货币为美元(200美元)。区域格式应为美国。

我在印度工作,这些变化是我的工作。如果不正确,请纠正我。





相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签