简体   繁体   中英

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 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.

Update, for reference;

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.

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.

(Math.Truncate(GetArea() * 100) / 100).ToString("N2");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM