如何将十进制值四舍五入到小数点后两位(用于在页面上输出)
问题描述:
当当前使用 .ToString()
显示小数的值时,准确地喜欢15个小数位,并且因为我使用它来表示美元和美分,我只希望输出为2个小数位。
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 cents, I only want the output to be 2 decimal places.
是否为此使用 .ToString()
的变体?
答
decimalVar.ToString ("#.##"); // returns "" when decimalVar == 0
或
decimalVar.ToString ("0.##"); // returns "0" when decimalVar == 0