|
-
Sep 9th, 2008, 09:59 AM
#1
Thread Starter
Member
[RESOLVED] Rounding Problem
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.
-
Sep 9th, 2008, 11:24 AM
#2
Re: Rounding Problem
Have you taken a look at VB6's ROUND function?
-
Sep 9th, 2008, 12:31 PM
#3
Re: Rounding Problem
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.
-
Sep 9th, 2008, 01:04 PM
#4
Thread Starter
Member
Re: Rounding Problem
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.
-
Sep 9th, 2008, 01:22 PM
#5
Lively Member
Re: Rounding Problem
If you divide by ten, take the integer/long then multiply by ten it should give you a number always ending in zero.
Cadman
-
Sep 9th, 2008, 01:27 PM
#6
-
Sep 9th, 2008, 01:36 PM
#7
Thread Starter
Member
Re: Rounding Problem
hi cadman
i have tried what u have told me but it was not working it will not give me values in zero
-
Sep 9th, 2008, 01:55 PM
#8
Lively Member
Re: Rounding Problem
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.
-
Sep 9th, 2008, 02:02 PM
#9
Lively Member
Re: Rounding Problem
Oops, you wanted 7560 not 7450.
7459.2
Try: round(7459.2/10)*10
-
Sep 9th, 2008, 02:04 PM
#10
Re: Rounding Problem
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
-
Sep 9th, 2008, 06:28 PM
#11
Re: Rounding Problem
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
-
Sep 9th, 2008, 09:14 PM
#12
Junior Member
Re: Rounding Problem
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.
Last edited by Alvanian; Sep 9th, 2008 at 09:31 PM.
-
Sep 9th, 2008, 09:30 PM
#13
Re: Rounding Problem
 Originally Posted by Alvanian
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 
Round to what? Reread the requirement.
-
Sep 10th, 2008, 07:39 AM
#14
Junior Member
Re: Rounding Problem
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
-
Sep 10th, 2008, 07:55 AM
#15
Thread Starter
Member
Re: Rounding Problem
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.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|