I have some calculated values like 5823, 7459.2 and i want to display it as
5820 ,7460 like that. i am display this values in data grid.
Please help in this.
Printable View
I have some calculated values like 5823, 7459.2 and i want to display it as
5820 ,7460 like that. i am display this values in data grid.
Please help in this.
Have you taken a look at VB6's ROUND function?
There are no built-in functions that will round 5823 down to 5820 or 7459.2 up to 7460 - anyone who's more or less familiar with elementary math knows why.
You will have to write your own function to do that. I think we have some samples posted so try searching.
Thanks for the Reply..
I have used round function but it will not help me in this case
Rhinobull do you have any function or any thread you know than please tell me.
If you divide by ten, take the integer/long then multiply by ten it should give you a number always ending in zero.
Cadman
hi cadman
i have tried what u have told me but it was not working it will not give me values in zero
What is your data type? Integers and longs should strip decimals out.
You may need to copy them into an integer or long first instead of single or double precision then do the divide, multiply as I suggested above.
So for a number such as 7459.2 as an integer would be 7459. Divide by ten would be 745. Mult by ten gives 7450.
Also the Fix function could be used to strip decimals like:
Fix(7459.2/10)*10 would give 7450.
Oops, you wanted 7560 not 7450.
7459.2
Try: round(7459.2/10)*10
Code:Private Sub Form_Load()
Dim lngValue As Long
lngValue = 5823 '7459.2
lngValue = Round(lngValue, 0)
If Right$(lngValue, 1) < 5 Then
MsgBox SymDown(lngValue, 0.1)
Else
MsgBox SymUp(lngValue, 0.1)
End If
End Sub
Function SymDown(ByVal X As Double, _
Optional ByVal Factor As Double = 1) As Double
SymDown = Fix(X * Factor) / Factor
End Function
Function SymUp(ByVal X As Double, _
Optional ByVal Factor As Double = 1) As Double
Dim Temp As Double
Temp = Fix(X * Factor)
SymUp = (Temp + IIf(X = Temp, 0, Sgn(X))) / Factor
End Function
Two way of rounding you can choose:
Code:'-- banker rounding
n = CLng(x / 10) * 10
'-- standard rounding: 0 to 4 round down, 5 to 9 round up
n = Int((x + 5) / 10) * 10
'-- or
n = ((x + 5) \ 10) * 10
Code:x1 = 5823
x2 = 5825
x3 = 5835
x4 = 7459.2
'-- banker rounding
n1 = CLng(x1 / 10) * 10 '-- gives 5823
Debug.Print x1, n1
n2 = CLng(x2 / 10) * 10 '-- gives 7460
Debug.Print x2, n2
n3 = CLng(x3 / 10) * 10 '-- gives 5820
Debug.Print x3, n3
n4 = CLng(x4 / 10) * 10 '-- gives 5840
Debug.Print x4, n4
Debug.Print
'-- standard rounding: 0 to 4 round down, 5 to 9 round up
n1 = Int((x1 + 5) / 10) * 10 '-- gives 5823
Debug.Print x1, n1
n2 = Int((x2 + 5) / 10) * 10 '-- gives 7460
Debug.Print x2, n2
n3 = Int((x3 + 5) / 10) * 10 '-- gives 5830
Debug.Print x3, n3
n4 = Int((x4 + 5) / 10) * 10 '-- gives 5840
Debug.Print x4, n4
Debug.Print
Just take advantage of Visual Basics rounding...
It can in fact round.
Anything declated as an integer will automatically round for you. So...
vb Code:
Dim X as integer x = The number you need rounded
Thats all :)
EDIT: whoops misread your starting post. This function will only work for rounding decimals.
Round to what? Reread the requirement.Quote:
Originally Posted by Alvanian
And you didnt read my post all the way through.
That only rounds decimal points. Even then thats still half the problem. From there you can just get the last digit in the number. Subtract it from 9 if its greater than 4 then add the difference +1 back.
Or if its less than 5 just subtract it from the whole. ill submit code later got work now
Thank You very much anhn your idea works for me
it gives me perfect values whatever i need
you are great.............
Thanks again..........
Thanks to all for your reply.