How do I display a decimal value to 2 decimal places?

前端 未结 17 2173
没有蜡笔的小新
没有蜡笔的小新 2020-11-21 23:24

When displaying the value of a decimal currently with .ToString(), it\'s accurate to like 15 decimal places, and since I\'m using it to represent dollars and ce

17条回答
  •  遇见更好的自我
    2020-11-22 00:09

    Very rarely would you want an empty string if the value is 0.

    decimal test = 5.00;
    test.ToString("0.00");  //"5.00"
    decimal? test2 = 5.05;
    test2.ToString("0.00");  //"5.05"
    decimal? test3 = 0;
    test3.ToString("0.00");  //"0.00"
    

    The top rated answer is incorrect and has wasted 10 minutes of (most) people's time.

提交回复
热议问题