Customized rounding problem

Ileia

New Member
Joined
Mar 12, 2010
Messages
2
I need to round a number as follows:
if the value behind the decimals is <.25 then it should round down to the whole number, if the value is >.25 but <.5 it should round up to .5, if the value is <.5 it should round up to the next whole number.

an example:
1.24 becomes 1
1.45 becomes 1.5
1.75 becomes 2

I've found this solution below, but need to add the rounding up part to the next whole number:
Function CustomRound(pValue As Double) As Double
Dim LWhole As Long
Dim LFraction As Double
'Retrieve integer part of the number
LWhole = Int(pValue)
'Retrieve the fraction part of the number
LFraction = pValue - LWhole
If LFraction < 0.25 Then
CustomRound = LWhole
Else
CustomRound = LWhole + 0.5
End If
End Function

Any help is highly appreciated. Thank you.
 

Some videos you may like

Excel Facts

What is the fastest way to copy a formula?
If A2:A50000 contain data. Enter a formula in B2. Select B2. Double-click the Fill Handle and Excel will shoot the formula down to B50000.

pgc01

MrExcel MVP
Joined
Apr 25, 2006
Messages
19,884
Hi Ileia
Welcome to the board

For ex., try:

Code:
Function CustomRound(pValue As Double) As Double
Dim LWhole As Long
Dim LFraction As Double
 
'Retrieve integer part of the number
LWhole = Int(pValue)
 
'Retrieve the fraction part of the number
LFraction = pValue - LWhole
 
' Round
If LFraction < 0.25 Then
    CustomRound = LWhole
ElseIf LFraction < 0.5 Then
    CustomRound = LWhole + 0.5
Else
    CustomRound = LWhole + 1
End If
 
End Function

Remark: shouldn't the variable name be DFraction instead of LFraction, since it's a Double (assuming the first letter is a type prefix)?
 
Last edited:

Watch MrExcel Video

Forum statistics

Threads
1,123,172
Messages
5,600,128
Members
414,365
Latest member
UUR

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
Top