如何格式化双结果和return?
How to format double result and return?
我想格式化结果 return,我该怎么做?
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.Floor
向下舍入,Math.Ceiling
向上舍入,Math.Truncate
向零舍入。因此,Math.Truncate
对于正数类似于 Math.Floor
,对于负数类似于 Math.Ceiling
。
为了完整起见,Math.Round
四舍五入到最接近的整数。如果数字恰好在两个整数之间,则它会向偶数方向舍入。
(Math.Truncate(GetArea() * 100) / 100).ToString("N2");
我想格式化结果 return,我该怎么做?
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.Floor
向下舍入,Math.Ceiling
向上舍入,Math.Truncate
向零舍入。因此,Math.Truncate
对于正数类似于 Math.Floor
,对于负数类似于 Math.Ceiling
。
为了完整起见,Math.Round
四舍五入到最接近的整数。如果数字恰好在两个整数之间,则它会向偶数方向舍入。
(Math.Truncate(GetArea() * 100) / 100).ToString("N2");