Round function

dealwi8me

Registered User.
Local time
, 01:52
Joined
Jan 5, 2005
Messages
187
Hello,

i have a problem with function round.

Result 1: round(3.215,2) --> 3.21 (fuction's result)
Result 2: round(3.215,2)-->3.22 (wanted result)

Is there a customization to round function in order to get the second result?

Thank you in advance.
 
How are you doing this? Also, what version of Access are you using? I just tried it in VBA using Access 2003 SP2 and it worked like you wanted.
 
It explains why that is happening in the link you supplied:

If expression is exactly halfway between two possible rounded values, the function returns the possible rounded value whose rightmost digit is an even number

If you want it to always round up when the last digit is 5 you could use a function like this:

Code:
Function user_round(sngNum As Single)

Dim strNum As String


If Right(CStr(sngNum), 1) = "5" Then
   strNum = Left(CStr(sngNum), Len(CStr(sngNum)) - 1)
   strNum = strNum & "6"
   sngNum = CSng(strNum)
End If

user_round = Round(sngNum, 2)

End Function
 
Last edited:
I will try the user_round function.

Thank you for your help :)
 
Use this. Then it will work for any precision:
Code:
Function CRound(MyVal As Double, Prec As Integer) As Double

 

    

    CRound = Int(MyVal * 10 ^ Prec + 0.5) / 10 ^ Prec

    

End Function
 
Use this. Then it will work for any precision:
Code:
Function CRound(MyVal As Double, Prec As Integer) As Double

  

    CRound = Int(MyVal * 10 ^ Prec + 0.5) / 10 ^ Prec
   

End Function

In this example above and I want to round my value to 2 decimal places

Currently if I use the round function where my result is
2.515 => 2.51

Desired Result
2.515 => 2.52

I saw the explanation where if the value is "odd", it rounds down but "even" it rounds up. So I get why you should watch out when using the ROUND function.

But I want to round up, so using the function above, the code below should resolve my problem correct?


Dim MyVal as double
Dim Cround as double
Dim Prec as integer

' Round to 2 decimal places
Prec = 2

MyVal = 50 * .0503
Cround = Int(MyVal * 10 ^ Prec + 0.5) /10 ^ Prec

Can anyone verify if I wrote this properly?

My Cround result does give me 2.52 but I want to make sure the "Prec" variable should represent how many decimals out I should represent.

BTW, I have no idea what the "^" carat symbol means.
 

Users who are viewing this thread

Back
Top Bottom