How do I make Rounding, for example:
0.125 must be: 1
1.75 must be: 2
2.05 must be: 3
5.118 must be: 6
Printable View
How do I make Rounding, for example:
0.125 must be: 1
1.75 must be: 2
2.05 must be: 3
5.118 must be: 6
Use Int or, if you have vb6 Fix.
To get information on using Fix, look up msdn.microsoft.comCode:Number = Int(Number + 1)
Look what I've found:
So instead useCode:Dim nr As Integer
nr = 1.9
MsgBox Int(nr + 1) 'Returns 3!
VB automaticly replaces the 0.sixteen 9's to 1#, thats how I discovered that :) cool huh?Code:Dim nr As Integer
nr = 1.99
Number = Int(nr + 1#)
'1# stands for 0.9999999999999999 (0.sixteen 9's)
'You have to use that 1# because otherwise you'll get:
nr = 1.00000000000001
MsgBox Int(nr + 0.999999999999999) '(0.fifteen 9's)
'Returns 1, not exactely what you want huh?
have fun man!
[Edited by Jop on 10-02-2000 at 01:26 PM]
Jop: In your first post you seemed surprised that Int(nr + 1) resulted in a value of 3. The reason that happens is that you defined nr as an Integer. When you attempted to store a non-integer value in it by writing nr = 1.9, VB made it an integer by rounding so it became = 2. The following illustrates that happening.
Dim nr As Integer
nr = 1.9
MsgBox nr
MsgBox Int(nr + 1) 'Returns 3!
Yeah I realised that, I was retesting it and it didn't seem to work :(
but now I have this strange thing:
how do I make it so it doesn't round it itself before processing?Code:Dim nr As Long 'LONG!
nr = 1.9
MsgBox nr '2
MsgBox Int(nr + 1#) '3
Long is shorthand for Long Integer so it's still an integer, just one that will strore larger numbers than 32, 767. Use Double or Currency.
Thanx, didn't knew about the Long Integer
I learn new things every day!
Or Single.Quote:
Originally posted by MartinLiss
Use Double or Currency.
What's the difference between double & single?
I think it something like this: (look at msdn if you want to be sure)
Double:14 decimals
Single:7 decimals
Thanks to you all ! I think I found a solution:
INT and FIX rounds downwards, so I make it this way:
Number! = Number! + 0.99
and it works !
Sorry , it must be:
Number! = FIX(number + 0.99)