Parsing decimal with thousands separator

后端 未结 2 1432
醉梦人生
醉梦人生 2021-01-21 06:12

I have the following block of code:

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


        
2条回答
  •  逝去的感伤
    2021-01-21 06:58

    The result of this binary and (&) will always be 0 (false, or NumberStyles.None). That's why it doesn't allow decimal and thousand separators:

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

    Change to binary or (|):

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

提交回复
热议问题