Round to the next higher number

I want to round a value (double) to the next (all rounding time) number. Rounding can be determined by any number.

Exp .:
Round to the next 2.50

0.00 --> 0.00 0.01 --> 2.50 2.49 --> 2.50 2.50 --> 2.50 2.50000001 --> 5.00 ... 

The algorithm for this is easy (if the "number" was negative * -1):

 Math.Round((Math.Abs(number) + tolerance) / 2.50, MidpointRounding.AwayFromZero) * 2.50 

Tolerance is defined as follows:

 tolerance = 2.50 / 2 - Math.Pos(10, -x); 

But I do not know how to determine x! Because in the case of the 1st-4th example, x should be 0.01 in the case of the 5th example, it should be 0.0000001 and so on ...

The search results only allow you to analyze the string of a decimal number and calculate the decimal digit. Is there a mathematical way? Otherwise, I will have to process various locale settings for the decimal separator and numbers without decimal digits (not required to remove the decimal separator).

Does anyone have a solution for my problem. Thanks!

Regards, Danny

+4
source share
4 answers

What about Math.Ceiling(v / 2.5) * 2.5 ?

+12
source

Math.Ceiling does exactly what you need.

0
source

You need Math.Ceiling

This takes a double and rounds it up to the nearest integer if the value is already equal to an integer. However, the returned data type is still double.

Usage example ...

 Double testValue = 1.52; Console.WriteLine(Math.Ceiling(testValue)); 

... will print 2 .

0
source

you can use math.ceiling for this

0
source

All Articles