I can round the elements of A
to the nearest integers greater than or equal to A
ceil(A)
But what about if I want to round it to the nearest 50 greater than or equal to A
?
For example, given the following A
array,
A=[24, 35, 78, 101, 199];
A subroutine should return the following
B=Subroutine(A)=[50, 50, 100, 150, 200];
To round a number up to nearest 0.5, use the CEILING function, for example =CEILING(A2, 0.5) . To round a number up or down to nearest 0.5, use the MROUND function, e.g. =MROUND(A2, 0.5) .
Round Up or Round Down to Nearest 100 You can use the ROUNDUP or ROUNDDOWN Functions to round a number up or down instead of using standard rounding rules.
Excel has a very elementary take on rounding. It's the same method you learned early in grade school: If you're rounding to a whole number, decimals from 0.1 to 0.4 round down and decimals from 0.5 to 0.9 round up.
You could just divide by 50, take ceil(), and multiply by 50 again:
octave:1> A=[24, 35, 78, 101, 199];
octave:2> ceil(A)
ans =
24 35 78 101 199
octave:3> 50*(ceil(A/50.))
ans =
50 50 100 150 200
An easy way is to just add each number's complement modulo 50:
octave> A = [24, 35, 78, 101, 199]
octave> mod(-A, 50) # Complement (mod 50)
ans =
26 15 22 49 1
octave> A + mod(-A, 50) # Sum to "next higher" zero (mod 50)
ans =
50 50 100 150 200
octave> A - mod(A, 50) # Can also sum to "next lower" zero (mod 50)
ans =
0 0 50 100 150
(Note that this only depends on integer arithmetic, which avoids errors due to floating-point rounding.)
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