Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Math function to round up the values to .0 or .5

Tags:

c#

asp.net

How can I round numbers up to the nearest whole or to the nearest half?

For Example:

> 23.15 --> 23.5
> 23.56 --> 24.0

The rounding functions I know about are floor and ceil, but those only round to the nearest integer.

like image 566
Prasad Avatar asked Oct 13 '12 04:10

Prasad


People also ask

How do I round up to the nearest 0.05 in Excel?

Excel: Round to the Nearest $0.05 with MROUND How do I round to the nearest nickel or quarter? Strategy: You can use the MROUND function. This function will round a number to the nearest multiple of the second argument. To round to the nearest nickel, use =MROUND(B2,0.05).


1 Answers

You want to round up, to a multiple of 0.5? Am I understanding that correctly?

double RoundUpToPointFive(double d)
{
    return Math.Ceiling(d * 2) / 2;
}
like image 89
David Yaw Avatar answered Sep 19 '22 11:09

David Yaw