I want to round a value (double) to the next (allways round up) number. Rounding can be defined by any number.
Exp.:
Round up 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 to do this is easy (if 'number' was negative * -1):
Math.Round((Math.Abs(number) + tolerance) / 2.50, MidpointRounding.AwayFromZero) * 2.50
Tolerance is defined like this:
tolerance = 2.50 / 2 - Math.Pos(10, -x);
But I don't know how to determine x! Because in case of the 1st-4th example x should be 0.01 in case of the 5th example it should be 0.0000001 and so on...
Search results only suggest to parse the string of a decimal number and count the decimal digit. Is there no mathematical way? Otherwise I have to treat with different locale settings for decimal seperator and numbers with no decimal digits (no decimal seperator to remove).
May anyone has a solution for my issue. Thank you!
Kind regards, Danny
Rounding Large Numbers When rounding numbers, if the digit to the right is 4 or less, we round down (we leave the number we are rounding the same). If the digit is 5 or higher, we round up (we increase the digit we are rounding by 1). Round to the indicated place value.
Here's the general rule for rounding: If the number you are rounding is followed by 5, 6, 7, 8, or 9, round the number up. Example: 38 rounded to the nearest ten is 40. If the number you are rounding is followed by 0, 1, 2, 3, or 4, round the number down.
C) Round 7.5 to the nearest whole number. 7.5 is exactly halfway between 7 and 8. Therefore, 7.5 rounds to 8. To round to the nearest tenth using a number line, look at the tenth place value closest to the number you are rounding.
For example, 1.5 (pronounced as "one point five" or "one and a half") would be rounded up to 2, and 2.1 would be rounded down to 2.
How about Math.Ceiling(v / 2.5) * 2.5
?
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With