String to decimal parsing C#

前端 未结 3 645
盖世英雄少女心
盖世英雄少女心 2021-01-21 13:44

I am trying to parse or convert a string to decimal in C#.

I need to be able to parse strings like,

$123,345,676.8999 to its equivalent 123345676.90.

相关标签:
3条回答
  • 2021-01-21 13:47

    You can use decimal.TryParse to perform the parsing -- the link has some samples on how the parsing works.

    0 讨论(0)
  • 2021-01-21 13:59

    Very simple answer: use Decimal.Parse()

    0 讨论(0)
  • 2021-01-21 14:05

    Try this:

    var val = double.Parse("$123,345,676.8999", NumberStyles.AllowThousands | NumberStyles.AllowDecimalPoint | NumberStyles.AllowCurrencySymbol);
    val = Math.Round(val, 2);
    
    0 讨论(0)
提交回复
热议问题