C# Decimal.Parse issue with commas

前端 未结 4 1090
情书的邮戳
情书的邮戳 2020-12-08 20:32

Here\'s my problem (for en-US):

Decimal.Parse(\"1,2,3,4\") returns 1234, instead of throwing an InvalidFormatException.

Most Windows application

4条回答
  •  Happy的楠姐
    2020-12-08 20:57

    It's allowing thousands, because the default NumberStyles value used by Decimal.Parse (NumberStyles.Number) includes NumberStyles.AllowThousands.

    If you want to disallow the thousands separators, you can just remove that flag, like this:

    Decimal.Parse("1,2,3,4", NumberStyles.Number ^ NumberStyles.AllowThousands)
    

    (the above code will throw an InvalidFormatException, which is what you want, right?)

提交回复
热议问题