如何格式化双重结果并返回? [英] How to format double result and return?
问题描述
我想格式化结果并返回,我该怎么做?
I want to format result and return, how i can do that?
public virtual double GetArea()
{
return Math.PI * this.radius * this.radius;
}
推荐答案
public virtual double GetArea()
{
return Math.Round(Math.PI * this.radius * this.radius, 2);
}
Math.Round
会将整数舍入到第二个参数中指定的小数位数-在这种情况下,它将舍入到小数点后两位.
Math.Round
will round a integer to the number of decimal places specified in the second parameter - in this instance, it will round to two decimal places.
更新,以供参考;
Math.Floor
向下舍入,Math.Ceiling
向上舍入,Math.Truncate
朝零舍入.因此,Math.Truncate
对于正数类似于Math.Floor
,对于负数类似于Math.Ceiling
.
Math.Floor
rounds down, Math.Ceiling
rounds up, and Math.Truncate
rounds towards zero. Thus, Math.Truncate
is like Math.Floor
for positive numbers, and like Math.Ceiling
for negative numbers.
出于完整性考虑,Math.Round
舍入到最接近的整数.如果数字恰好在两个整数之间,则将其舍入为偶数.
For completeness, Math.Round
rounds to the nearest integer. If the number is exactly midway between two integers, then it rounds towards the even one.
这篇关于如何格式化双重结果并返回?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!