complex rounding decimals in excel spreadsheet - excel

Have created formula for example =(E2+(G2*37))/290 which returns decimals based on data entered but can't work out how to round the answer to get the following:
If 0 to 0.39 round to 0
If 0.4 to 0.89 round to 0.5
If 0.9 to 1.39 round to 1
If 1.4 to 1.89 round to 1.5
If 1.9 to 2.39 round to 2 etc
Hope my question makes sense. Thanks

Your custom rounding is simply rounding to the nearest 0.5.....but with an "offset" of 0.15. With value to be rounded in A1 you can do that with a simple formula, i.e.
=ROUND(A1*2-0.3,0)/2
or with your calculation in place of A1 that becomes
=ROUND((E2+G2*37)/290*2-0.3,0)/2

It's a bit convoluted but this should work:
=IF(A1 + 0.1 - ROUNDDOWN(A1+0.1;0) < 0.5; ROUNDDOWN(A1+0.1;0); ROUNDDOWN(A1+0.1;0) + 0.5)
where A1 is the cell you want to round.
e.g.
N.B.
This works only for positive numbers.
Ranges are actually:
[0, 0.39999999...] [0.4 , 0.8999999...] ...
or equally:
[0, 0.4) [0.4 , 0.9) ...

You could define a VBA function to do this for you:
Public Function CustomRound(number As Double)
Dim result As Double, fraction As Double
fraction = number - Int(number)
If number <= 0.39 Then
result = 0
ElseIf fraction >= 0.4 And fraction <= 0.9 Then
result = Int(number) + 0.5
Else
result = Round(number, 0)
End If
CustomRound = result
End Function
You would call this as follows:
=CustomRound((E2+(G2*37))/290)
Example:
=CustomRound(0.23) // 0
=CustomRound(1.58) //1.5
=CustomRound(2.12) //2

Related

How Can I Round Prices to the nearest 0.95 with an Excel Formula?

I need to round up all my prices to the nearest $0.95. For instance I have 23.106 and 102.58888. They would need to be rounded off to $22.95 and $102.95. How can I do this with an excel formula/function?
Try this:
=IF(OR(A3-FLOOR(A3,1)>0.95,A3=CEILING(A3,1)),CEILING(A3,1)+1,CEILING(A3,1))-0.05
If the decimal is greater than .95 or is a whole number (no decimals), it will add add 1 to the number and then use the CEILING function to round the number up and then subtract .05.
1.52 = 1.95
2.00 = 2.95
66.98 = 67.95

How can 0.2 + 0.1 be equal to 0.3 in Excel?

I understand perfectly why 0.1 + 0.2 is not equal to 0.3 due to the floating point. In most of programming languages, 0.1 + 0.2 == 0.3 is False.
But in Excel if(0.1 + 0.2 == 0.3; 1; 0) gives 1
The reason this happens in Excel is because Excel only keeps track of 15 digits of precision. Floating point math for 0.2 + 0.1 results in 0.30000000000000004, and that 4 way out there is the 17th digit. That means Excel just truncates everything after the 15th 0 and is left with 0.300000000000000 which = 0.3
See here for more info: https://en.wikipedia.org/wiki/Numeric_precision_in_Microsoft_Excel

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

EXCEL How to write a step function

How do you return different values in a cell based on which range the value entered in another cell comes under? Specifically, I am trying to make a step function.
For example:
IF G2 is ABOVE "0" BUT BELOW "1" THEN display "0.1"
IF G2 is ABOVE "0.99" BUT BELOW "5" THEN display "0.15"
IF G2 is ABOVE "4.99" BUT BELOW "15" THEN display "0.2"
IF G2 is ABOVE "14.99" BUT BELOW "30" THEN display "0.5"
IF G2 is ABOVE "29.99" BUT BELOW "100" THEN display "1.0"
IF G2 is ABOVE "99.99" THEN display "1.30"
So IF G2 was "£18.75" then the cell that this formula is entered in would display "£0.50" based on the value's above.
(bear in mind that this is specific to my spreadsheet and was for calculating prices i.e. 0.99 = £0.99)
Following #oli_taz's suggestion, here is a slightly more robust solution that can deal with any input:
=IF(D4<F4, 0, VLOOKUP(D4,F4:G9,2))
with the range F4:G9:
0 0.1
1 0.15
5 0.2
15 0.5
30 1
100 1.3
and D4 being the value in question, e.g. 18.75 -> result: 0.5
Numbers smaller than 0 will return 0 and numbers larger than 100 will return 1.3.
Nested if's in Excel Are ugly:
=If(G2 < 1, .1, IF(G2 < 5,.15,if(G2 < 15,.2,if(G2 < 30,.5,if(G2 < 100,.1,1.3)))))
That should cover it.

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