如何将十进制值四舍五入到小数点后两位(用于在页面上输出) [英] How do I round a decimal value to 2 decimal places (for output on a page)

查看:58
本文介绍了如何将十进制值四舍五入到小数点后两位(用于在页面上输出)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当当前使用 .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

这篇关于如何将十进制值四舍五入到小数点后两位(用于在页面上输出)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆