C# Decimal.Parse issue with commas

前端 未结 4 1087
情书的邮戳
情书的邮戳 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条回答
  •  粉色の甜心
    2020-12-08 20:58

    It is a common issue never solved by microsoft. So, I don't understand why 1,2,3.00 (english culture for example) is valid! You need to build an algorith to examine group size and return false/exception(like a failed double.parse) if the test is not passed. I had a similar problem in a mvc application, which build in validator doesn't accept thousands..so i've overwrite it with a custom, using double/decimal/float.parse, but adding a logic to validate group size.

    If you want read my solution (it is used for my mvc custom validator, but you can use it to have a better double/decimal/float.parse generic validator) go here https://stackoverflow.com/a/41916721/3930528

提交回复
热议问题