Convert decimal to string without commas or dots

后端 未结 8 1100
盖世英雄少女心
盖世英雄少女心 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条回答
  • 2021-01-12 07:54

    I would say this may help you: var res = amount.ToString().Replace(".", "").Replace(",", ""); :)

    0 讨论(0)
  • 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.

    0 讨论(0)
提交回复
热议问题