C# Decimal Formatting Query

后端 未结 4 1560
醉话见心
醉话见心 2021-01-15 04:49

I am trying to format a decimal so that it will get displayed as so:

14.5 should get displayed as \"14.50\" 14.50 should get displayed as \"14.50\" 14.05 should get

4条回答
  •  攒了一身酷
    2021-01-15 05:30

    Yes. You can use the "G" format specifier. For samples, see String.Format's documentation.

    For example:

    decimal value = 14.05m;
    string result = String.Format("{0:G}", value); // "14.05"
    
    value = 14m;
    result = String.Format("{0:G}", value); // "14"
    

提交回复
热议问题