Difficulty with unconventional rounding - rounding

I have a vector of values rounded to the nearest 10th, e.g
26.8, 23.3, 24.6, 27.1
I need to round each value to the nearest 0.5, in increments of 1, e.g.
26.5, 23.5, 24.5, 27.5
NOT to the nearest 0.5 in increments of 0.5, e.g.
27.0, 23.5, 24.5, 27.0
Any suggestions would be greatly appreciated. Thank you.
Cheers,
Carly

Truncate and add 0.5.
The numbers in the interval [x.000, x.999] are truncated to x.0 and then adding 0.5 puts them at x.5. This is sufficient for every interval, assuming that no extra requirements are placed on integer starting values.

Multiply your number by 2, round it, then divide by 2 back.

Related

Generate a random range of numbers in excel

I'm looking for an excel formula to generate a random range of numbers between -0.3 and +0.3. These numbers can be at most 2 decimal places.
E.g here is a list of numbers I would expect this formula to return:
-0.24, 0.02, 0.13, -0.14, 0.3, 0.22, -0.29
and so on...
Many thanks
thats quite easy to do:
=RANDBETWEEN(-30;30)/100

Rounding up and rounding down in excel

I don't know how to round up and round down in excel if I have variety of numbers.
For ex: If I have 9.233, then it rounds down to 9 because 0.233 < 0.5, but if it is 2.457, then it rounds up to 3 because 0.457 = 0.5 (if you round it), and then if I have 3.890, then it rounds up to 4 because 0.890 >= 0.5.
I don't know what to do. I tried:
=IF(A1-INT(A1)>0.1,ROUNDUP(A2,0),INT(A2))
but it doesn't work.
Please help
Have you tried just the ROUND(, 0) for your purpose.
This should yeild the desired result.
Num Rounded Num
-----------------------
9.233 9
2.457 2
3.89 4

origin value and after sum after ROUND() is different

I have a table with "ordered amount", "percentages", and "total sum". Note that "ordered amount" and "total sum" should be the same.
column F= sum of C:F
*rows 4,6,8,10 used =round()
As shown in the table(image), for some numbers, value in F is not the same as A when it should be. (e.g. A6(105) and F7(104), -1 after rounding).
Is there anyway to avoid this?
Thank you very much.
If the sum of the decimal points adds up to > 5, your final rounded number will round up. If they add up to < 5, the final number will round down.
Consider this example:
10.5 + 5 = 15.5, which rounds to 16.
However
10 + 5 = 15
This is what is happening in your table.
In order to get the sum of rounded numbers to add up to the same as the sum of the numbers, in your example, you will need to NOT round, but rather subtract, one of the values. I would suggest altering the largest value, as it would seem to have the least effect on the percentages, but that is a choice you can make.
To do that, with your data in row 8, for example you could do the following.
A9: =IF(B$8=MAX($B$8:$E$8),SUM($B$8:$E$8)-SUM(ROUND($B$8:$E$8,0))+ROUND(B$8,0),ROUND(B$8,0))
entered as an array formula with ctrl+shift+enter and fill right to E9.
This would give a sum of 344 which is the same as F8

Choose a random value between 0.00 and 20.00

In Excel, it is possible to select a random value from a set of 5 options in the following manner:
Values
e.g. 15, 30, 50, 75, or 100
Formula
=CHOOSE(RANDBETWEEN(1,5),15,30,50,75,100)
If I wanted to select a value from a much denser range how would I do it?
e.g. 0.00, 0.01, 0.02, 0.03 ,0.04 ,0.05 ... 19.95, 19.96, 19.97, 19.98, 20.00
What would be the correct formulae?
Consider the following formula:
=0.01*RANDBETWEEN(0,2000)
This will produce random multiples of .01
Scale the output of the random function by multiplying/dividing it by a constant.
For instance, if your Random function outputs floating decimal values between 0 and 1, and you need outputs between 0 and 100, multiply the output of the random function by 100.
If you need the final result to be an integer, you can then round the value to the nearest integer.
It sounds like you want
=ROUND(RAND()*20,2)
The 20 is the maximum value of your zero-to-maximum range, and the 2 is how many decimal places it'll round the final output to.

Google Spreadsheet - Round down at 0.5, but round up above that

The regular "ROUND" function will round down when < 0.5, and will round up when >= 0.5
I need 0.5 to be rounded down, but anything above that to be rounded up.
So:
10.4 should be 10
10.5 should be 10
10.6 should be 11
Edit: Here is the solution i came up with
If the value to be rounded is in B1
And the decimal precision is in A1 (0 = no decimals, 1 = one decimal place, etc)
=IF(MOD(ABS(B1),(1/(10^A1)))<=0.5*(1/(10^A1)),ROUNDDOWN(B1,A1),ROUNDUP(B1,A1))
The ABS() makes sure it works with negative numbers.
The (1/(10^A1)) makes sure that my precision (which is a second argument to Google's rounding functions) scales my boundary condition (0.5) accordingly.
And the MOD() is what actually determines my boundary condition.
Edit2:
More elegant solution thanks to #Jayen
=ROUNDUP(B1 - sign(B1) * (10 ^ -A1) / 2, A1)
It is possible to create an IF statement that would do this =IF(A1-int(A1)>0.5,int(A1)+1,int(A1))
But seams a strange request, the normal convention (in the west) is to round .5 up, not down.
Warning: this credited 'solution' has a bug:
=ROUNDUP(B1 - sign(B1) * (10 ^ -A1) / 2, A1)
Plug in .1, .2, .3, .4 or negative values of those to see the unintended results. The solution i went with is:
=ROUNDUP(MAX(ABS(B1)-1/2,0))*SIGN(B1)
So i used Jayen's clever formula, but used MAX with the ABS to eliminate the bug, then multiplied by the SIGN to allow it to work for positive and negative numbers.
I'm creating C1 = (10 ^ -A1) / 2 which is 0.5 if you round to 0 decimal places, 0.05 if you round to 1, etc.
Then it is simply:
=ROUNDUP(B1 - C1, A1)
Or substituting:
=ROUNDUP(B1 - (10 ^ -A1) / 2, A1)
EDIT:
Not sure if you want negative half numbers to round towards or away from 0, but is this ok?
=ROUNDUP(B1 - sign(B1) * (10 ^ -A1) / 2, A1)
That would be rounding towards 0 on the half.
EDIT2:
But in case you want negative half numbers to round away from 0 (all half numbers round towards negative infinity):
=CEILING(B1 - 10 ^ -A1 / 2, 10 ^ -A1)
EDIT3:
#ShawnKovac found an issue with my original answer when B1 < C1, so I've taken his and adapted it for any A1:
=ROUNDUP(MAX(ABS(B1) - 10 ^ -A1 / 2, 0), A1) * SIGN(B1)
Also my answer for rounding towards negative infinity throws an error when B1 < C1, so here's an update:
=CEILING(B1 - 10 ^ -A1 / 2, SIGN(B1 - 10 ^ -A1 / 2) * 10 ^ -A1)
Depending on the dataset, you could just subtract from your value, so long as you know that none of your numbers would be invalidated by doing so.
=round(A1 - 0.00001)
=IF(AND(LEN(MOD(A2,1)>4),RIGHT(A2,1)="5"),ROUNDDOWN(A2,2),ROUND(A2,2))
The mod 1 will leave just the 0. and the decimal places. So if 3 decimal places and you want to round to 2, and the decimal has a length greater than 4 ("0." plus decimals), this would indicate the decimal needs rounding. If the last digit of the decimal is 5 then use the rounddown feature else use round.
well if you just subtract 0.1 it will work with positive but if you use negative you can use if A1 is lower then 0 add 0.1
IF(A1<0,ROUND(A1+0.1,0),ROUND(A1-0.1,0))
A Table to show the result of the formula above ^^^^^
Hope it helped :)

Resources