Round value

mfuada

Registered User.
Local time
Today, 07:56
Joined
Feb 4, 2009
Messages
63
Hi.. i have this code . my plan if i have value of x = 10,50 then it will round down into value x = 10, but if x = 10,51 then it will round up into x=11
so can anyone help me?
this is my code :
Function RoundPriceSPN(varNilai As Double) As Double
Dim x As Double
If varNilai <= 0.5 Then
RoundPriceSPN = RoundD(varNilai)
Else
If varNilai > 0.5 Then
RoundPriceSPN = RoundU(varNilai)
End If
End If

RoundD is from roundDown function in excel and RoundU is from roundUp function in excel too...

Thx
 
Hi,

I could be wrong, but because .50 is the halfway mark, it will always round up, the value to the right of the decimal point has got to be less than .50 in order for it to round down.

You may have to write code that specifically deals with what your trying to achieve without the use of the Round Up or Round Down functions.

John
 
Last edited:
mfuada

Here is some coding that I used for rounding. Works quite well. Not sure who gave it to me though.

See attachement
 

Attachments

Try this also

ROUND((YourData)/5,2)*5

This gives you:

34.11 34.1
34.12 34.1
34.13 34.15
34.14 34.15
34.15 34.15
34.16 34.15
34.17 34.15
34.18 34.2
34.19 34.2
 
Thank you so much guys for the replies....
But i think my problem is that the function read the "0.5" value and NOT the value that i entered on my access form.. so if i input 0.50 the the function will round down the value into "0" and if i input the value 0.51 the function will round up to value "1", but if i entered the value such as 10.50 then the function wouldn't read the value and does not execute the round function (cause it always read to 0.50 & 0.51 values)
so any ideas guys to read a value in the textbox until 2 decimal behind zero?
thx
 
:confused:

But i think my problem is that the function read the "0.5" value and NOT the value that i entered on my access form..

Which Function?

Brian
 
Thank you so much guys for the replies....
But i think my problem is that the function read the "0.5" value and NOT the value that i entered on my access form.. so if i input 0.50 the the function will round down the value into "0" and if i input the value 0.51 the function will round up to value "1", but if i entered the value such as 10.50 then the function wouldn't read the value and does not execute the round function (cause it always read to 0.50 & 0.51 values)
so any ideas guys to read a value in the textbox until 2 decimal behind zero?
thx

You're right - the function you wrote will treat all numbers less than 0.5 one way, and everything else the other way.

If you want to modify your function to do rounding on the decimal part of the number, you need something like this:

Function RoundPriceSPN(varNilai As Double) As Double
Dim x As Double
If (varNilai - int(varNilai)) <= 0.5 Then
RoundPriceSPN = RoundD(varNilai)
Else
If (varNilai - int(varNilai)) > 0.5 Then
RoundPriceSPN = RoundU(varNilai)
End If
End If

But I can't help thinking you'd be better off using a built-in function such as Round()

(By the way, the lines of code in your function that I've highlighted in purple are unnecessary)
 

Users who are viewing this thread

Back
Top Bottom