Convert decimal to string without commas or dots

后端 未结 8 1102
盖世英雄少女心
盖世英雄少女心 2021-01-12 07:15

In .NET, I need to convert a decimal amount (money) to a numbers-only string, i.e: 123,456.78 -> 12345678

I thought

var dotPos = amount.ToString().L         


        
8条回答
  •  -上瘾入骨i
    2021-01-12 07:57

    You could do it like this:

    var amountString = string.Join("", amount.Where(char.IsDigit));
    

    Using the char.IsDigit method will protect you from other unknown symbols like $ and will also work with other currency formats. The bottom line, you don't know exactly what that string will always look like so it's safer this way.

提交回复
热议问题