Round But .5 Should Be Floored

round but .5 should be floored

Per Dietrich Epp's comment, you can use the ceiling() function with an offset to get a fast, vectorized, correct solution:

round_down <- function(x) ceiling(x - 0.5)
round_down(seq(-2, 3, by = 0.5))
## [1] -2 -2 -1 -1 0 0 1 1 2 2 3

I think this is faster and much simpler than many of the other solutions shown here.

As noted by Carl Witthoft, this adds much more bias to your data than simple rounding. Compare:

mean(round_down(seq(-2, 3, by = 0.5)))
## [1] 0.2727273
mean(round(seq(-2, 3, by = 0.5)))
## [1] 0.4545455
mean(seq(-2, 3, by = 0.5))
## [1] 0.5

What is the application for such a rounding procedure?

What is the Difference between Floor and Round

floor() will simply drop decimal value and return only integer.

So floor(1.2) => 1 and floor(1.9) => 1.

Meanwhile round() will round number that has decimal value lower than 0.5 to lower int, and when more than 0.5 to higher int:

So round(1.2) => 1 but round(1.9) => 2

Also round() has more options, like precision and rounding options.


Example:

$nums = [-1.5, -1, -.8, -.4, 0, .4, .8, 1, 1.5];

echo " \tround\tfloor\tceil" . PHP_EOL;
foreach ($nums as $a) {
echo $a . ": \t" . round($a) . "\t" . floor($a) . "\t" . ceil($a) . PHP_EOL;
}

/*
round floor ceil
-1.5: -2 -2 -1
-1: -1 -1 -1
-0.8: -1 -1 -0
-0.4: -0 -1 -0
0: 0 0 0
0.4: 0 0 1
0.8: 1 0 1
1: 1 1 1
1.5: 2 1 2

*/

Using Floor/Ceil to round decimals to integer of 5

To round to a certain number of decimal places, use this:

def my_round(x: float, round_to: float = 5, dp: int = 0) -> float: 
round_to *= 10**dp
x *= 10**dp
return ((x + round_to / 2) // round_to * round_to) / 10**dp

Testing it:

values = [702.500, 702.222, 707.466, 703.021]
targets = [705., 700., 705., 705.]
assert [my_round(x, 5, 0) for x in values] == targets
assert my_round(3.25, 0.1, 1) == 36.3

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

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;
}

Round to 5 (or other number) in Python

I don't know of a standard function in Python, but this works for me:

Python 3

def myround(x, base=5):
return base * round(x/base)

It is easy to see why the above works. You want to make sure that your number divided by 5 is an integer, correctly rounded. So, we first do exactly that (round(x/5)), and then since we divided by 5, we multiply by 5 as well.

I made the function more generic by giving it a base parameter, defaulting to 5.

Python 2

In Python 2, float(x) would be needed to ensure that / does floating-point division, and a final conversion to int is needed because round() returns a floating-point value in Python 2.

def myround(x, base=5):
return int(base * round(float(x)/base))

Javascript: Round up to the next multiple of 5

This will do the work:

function round5(x)
{
return Math.ceil(x/5)*5;
}

It's just a variation of the common rounding number to nearest multiple of x function Math.round(number/x)*x, but using .ceil instead of .round makes it always round up instead of down/up according to mathematical rules.



Related Topics



Leave a reply



Submit