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

前端 未结 17 2547
没有蜡笔的小新
没有蜡笔的小新 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-21 23:55

    decimalVar.ToString("F");
    

    This will:

    • Round off to 2 decimal places eg. 23.45623.46
    • Ensure that there are always 2 decimal places eg. 2323.00; 12.512.50

    Ideal for displaying currency.

    Check out the documentation on ToString("F") (thanks to Jon Schneider).

提交回复
热议问题