如何格式化双重结果并返回? [英] How to format double result and return?

查看:87
本文介绍了如何格式化双重结果并返回?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想格式化结果并返回,我该怎么做?

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屋!

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