简体   繁体   English

用千位分隔符解析小数

[英]Parsing decimal with thousands separator

I have the following block of code: 我有以下代码块:

string price = "1,234.56";
decimal value = 0;
var allowedStyles = (NumberStyles.AllowDecimalPoint & NumberStyles.AllowThousands);

if (Decimal.TryParse(price, allowedStyles, CultureInfo.InvariantCulture, out value))
{
    Console.log("Thank you!");
}
else
{
    throw new InvalidFormatException();
}

Ultimately, price will either be in US style (ie 1,234.56) or German style (ie 1.234,56). 最终, price将采用美式(即1,234.56)或德式(即1.234,56)。 My challenge is right now, Decimal.TryParse fails. 我现在面临的挑战是, Decimal.TryParse失败了。 I suspect its because of the thousands separator. 我怀疑它是因为千位分隔符。 Which is why I added the allowedStyles variable. 这就是我添加allowedStyles变量的原因。

What am I doing wrong? 我究竟做错了什么?

If you AND -combine the NumberStyles -flag, you will get None . 如果你 -combine的NumberStyles -flag,你会得到None

00100000 (AllowDecimalPoint)
&
01000000 (AllowThousands)
--------
00000000 (None)

Try to OR -combine them: NumberStyles.AllowDecimalPoint | NumberStyles.AllowThousands 尝试对它们进行OR组合: NumberStyles.AllowDecimalPoint | NumberStyles.AllowThousands NumberStyles.AllowDecimalPoint | NumberStyles.AllowThousands

00100000 (AllowDecimalPoint)
|
01000000 (AllowThousands)
--------
01100000 (AllowDecimalPoint, AllowThousands)

Additionally, I'm afraid that you can't parse both styles (US style and DE style) with one statement. 另外,我担心你不能用一个语句解析这两种样式(美式和DE样式)。

So I'd try both: 所以我试试两个:

string price = "1,234.56";
decimal value = 0;
var allowedStyles = (NumberStyles.AllowDecimalPoint | NumberStyles.AllowThousands);

if (Decimal.TryParse(price, allowedStyles, CultureInfo.GetCultureInfo("DE-de"), out value))
{
    Console.Write("Danke!");
}
else if (Decimal.TryParse(price, allowedStyles, CultureInfo.GetCultureInfo("EN-us"), out value))
{
    Console.Write("Thank you!");
}
else
{
    throw new InvalidFormatException();
}

The result of this binary and ( & ) will always be 0 ( false , or NumberStyles.None ). 此二进制文件and& )的结果将始终为0falseNumberStyles.None )。 That's why it doesn't allow decimal and thousand separators: 这就是为什么它不允许十进制和千位分隔符:

var allowedStyles = (NumberStyles.AllowDecimalPoint & NumberStyles.AllowThousands);

Change to binary or ( | ): 更改为二进制or| ):

var allowedStyles = (NumberStyles.AllowDecimalPoint | NumberStyles.AllowThousands);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 格式化double - 千位分隔符,带小数但没有尾随零 - Format double - thousands separator with decimal but no trailing zeros 编辑时小数列的 RadGridView 千位分隔符 - RadGridView Thousands Separator for Decimal Column While Editing 格式编号,带有3个尾随小数位,一个十进制千位分隔符,以及之后的逗号 - format number with 3 trailing decimal places, a decimal thousands separator, and commas after that Blazor 中自定义 inputText 组件中十进制字段的千位分隔符 - thousands separator for a decimal field in a custom inputText component in Blazor 如何更改 Excel C # 中的小数和千位分隔符? - How to change separator for decimal and thousands in numbers in Excel C #? 使用TypeConverter.ConvertFrom用千位分隔符解析数字类型 - Parsing Numeric Types with Thousands Separator using TypeConverter.ConvertFrom 在C#中导出到excel时如何使用正确的十进制分隔符和千位分隔符? - How to use the correct Decimal separator and Thousands separator when exporting to excel in c#? 在代码中使用千位分隔符 - Using thousands separator in the code 在 MVC 中放置千位分隔符 - Put thousands separator in MVC 在CurrentUICulture中指定千位分隔符 - Specify thousands separator in CurrentUICulture
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM