How to round a number DOWN to the nearest "log multiple"?

Kelvin Stott

Active Member
Joined
Oct 26, 2010
Messages
338
I created this formula which magnificently rounds a number UP to the nearest "log multiple" of 1, 1.5, 2, 2.5 or 5:

=CEILING(B4,10^INT(LOG(4*B4))/2)

Now I would like to modify this so that it rounds a number DOWN to the nearest "log multiple" of 1, 1.5, 2, 2.5 and 5, so I tried the following:

=FLOOR(B4,10^INT(LOG(4*B4))/2)

But this doesn't work. You'll see in the attached chart that there are gaps when it should be symmetric, and I don't understand why. Problem is I don't even understand how or why the first formula works, as I created that by trial and error.

File: https://drive.google.com/file/d/0B4rFQKgfCtswakp3ZEtwcFNwZXM/view?usp=sharing

Can anyone please help me fix this, so that I can round down as well as up.

Thanks!
 
Last edited:

Excel Facts

How to total the visible cells?
From the first blank cell below a filtered data set, press Alt+=. Instead of SUM, you will get SUBTOTAL(9,)
Here's an image of the chart, showing the gaps in the grey line (FLOOR function) where it should mirror the orange line (CEILING function):

[can't attach image, sorry]
 
Last edited:
Upvote 0
Can you explain what those bounds are are supposed to be?
 
Upvote 0
Yes, they are for scaling a chart axis automatically, but not the way Excel usually does it (going all the way down to zero, which I don't want).
 
Last edited:
Upvote 0
Is this what you need:

C2: =FLOOR(A2,10^INT(LOG(2*A2))/2)

ABC
1NumberRoundUpRoundDown
2111
31.11.51
41.41.51
51.51.51.5
61.621.5
71.921.5
8222
92.12.52
102.652.5
116105
12111510
13162015
14212520
15265025
165110050
17101150100
18151200150
19201250200
20251500250
215011000500
22100115001000

<tbody>
</tbody>
 
Upvote 0
OK, this is really strange. Again, I found this by trial and error, and I have NO idea why it works:

=FLOOR(B4,MIN(10^INT(LOG(4*B4))/2,10^INT(LOG(20*B4))/4))

And also this:

=MIN(FLOOR(B4,10^INT(LOG(2*B4))/2),FLOOR(B4,10^INT(LOG(4*B4))/4))

WHY? Nothing makes sense to me anymore! :confused:
 
Last edited:
Upvote 0
Looks great, but I tried that and it doesn't give the right floor (should be 25) from 30 to 49.

Sorry, my mistake. Clearly, I didn't test very comprehensively.

OK, this is really strange. Again, I found this by trial and error, and I have NO idea why it works:

=FLOOR(B4,MIN(10^INT(LOG(4*B4))/2,10^INT(LOG(20*B4))/4))

And also this:

=MIN(FLOOR(B4,10^INT(LOG(2*B4))/2),FLOOR(B4,10^INT(LOG(4*B4))/4))

WHY? Nothing makes sense to me anymore! :confused:

Given you're mostly working with increments of 2.5, 25, 250 etc in a log 10 system, there will be any number of combinations of log/4, and 4*log that will give you the right roundings.

I'd prefer myself to set out the logic in VBA like this, as it's then much easier to generalise, especially if your roundings are less uniform.

Code:
Function MyRound(d As Double, Optional bDown As Boolean = True) As Double

    Dim dInc As Double, dtemp As Double
    
    dtemp = Application.Log(d)
    Select Case 10 ^ (dtemp - Int(dtemp))
    Case Is <= 2.5
        dInc = 0.5
    Case Is <= 5
        dInc = 2.5
    Case Else
        dInc = 5
    End Select
    
    If bDown Then
        MyRound = Application.Floor(d, dInc * 10 ^ Int(dtemp))
    Else
        MyRound = Application.Ceiling(d, dInc * 10 ^ Int(dtemp))
    End If
    
End Function

LogScale: B2
A4: =10^LogScale
A5: =A4+10^(LogScale-1)
B4: =MyRound(A4)
C4: =MyRound(A4,FALSE)

In the cell highlighted in red, your ceiling formula gives 50.

ABC
1LogScale1
2
3NumberRoundDownRoundUp
4101010
5111015
6121015
7131015
8141015
9151515
10161520
11171520
12181520
13191520
14202020
15212025
16222025
17232025
18242025
19252525
20262550
21272550
22282550
23292550
24302550
25312550
26322550
27332550
28342550
29352550
30362550
31372550
32382550
33392550
34402550
35412550
36422550
37432550
38442550
39452550
40462550
41472550
42482550
43492550
44505050
455150100
465250100
475350100
485450100
495550100
505650100
515750100
525850100
535950100
546050100
556150100
566250100
576350100
586450100
596550100
606650100
616750100
626850100
636950100
647050100
657150100
667250100
677350100
687450100
697550100
707650100
717750100
727850100
737950100
748050100
758150100
768250100
778350100
788450100
798550100
808650100
818750100
828850100
838950100
849050100
859150100
869250100
879350100
889450100
899550100
909650100
919750100
929850100
939950100
94100100100

<tbody>
</tbody>
 
Upvote 0
Thanks Stephen, that makes a lot of sense. Probably I will steal/borrow your VBA code, since at least I understand it and so it's easy to modify.

Thanks again!
 
Upvote 0

Forum statistics

Threads
1,215,043
Messages
6,122,816
Members
449,095
Latest member
m_smith_solihull

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top