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

前端 未结 17 2196
没有蜡笔的小新
没有蜡笔的小新 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:11

    https://msdn.microsoft.com/en-us/library/dwhawy9k%28v=vs.110%29.aspx

    This link explains in detail how you can handle your problem and what you can do if you want to learn more. For simplicity purposes, what you want to do is

    double whateverYouWantToChange = whateverYouWantToChange.ToString("F2");
    

    if you want this for a currency, you can make it easier by typing "C2" instead of "F2"

提交回复
热议问题