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.
 

Excel Facts

Can you sort left to right?
To sort left-to-right, use the Sort dialog box. Click Options. Choose "Sort left to right"
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:
Upvote 0

Forum statistics

Threads
1,215,529
Messages
6,125,344
Members
449,219
Latest member
Smiqer

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