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

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

    I know this is an old question, but I was surprised to see that no one seemed to post an answer that;

    1. Didn't use bankers rounding
    2. Didn't keep the value as a decimal.

    This is what I would use:

    decimal.Round(yourValue, 2, MidpointRounding.AwayFromZero);
    

    http://msdn.microsoft.com/en-us/library/9s0xa85y.aspx

提交回复
热议问题