Link to home
Start Free TrialLog in
Avatar of Sting79
Sting79

asked on

control the decimal of an integer no.

i have a variable reciece a number like 11.45 or 11.56,
the problem i wana to know to control the decimal fraction (0.45 or 0.56) to do another operation like if the fraction is >0.5 then 11 will be 11 and if the fraction is <0.5 then 11 remain 11  
Avatar of Brian Mulder
Brian Mulder
Flag of Netherlands image

Hi Sting79
you can use int but it will not always work so use a wrapper there was a piece on this on one of the VB sites but it seems gone now so here's the main thing about it

the bug decribed by MS
http://support.microsoft.com/support/kb/articles/Q138/5/22.asp

Public Function mInt(ByVal Value As Double) As Integer
  mInt = Int(Value)
End Function

:O)Bruintje
ASKER CERTIFIED SOLUTION
Avatar of Brian Mulder
Brian Mulder
Flag of Netherlands image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
should test it better ROUND will do what you want
Hi Sting79,

As also already suggested by bruintje, If what you want is like:

11.50 -> 12
11.51 -> 12
11.49 -> 11

Then Round(11.xx) should work. If you want 11.50 to be 11 then do this:

Round(11.xx - .01).

Hope this helps.
Avatar of aikimark
beware of different rounding algorithms
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/odeopg/html/deconconversionroundingtruncation.asp

There are also some ExpertsExchange question threads on this same question.
The link supplied by aikimark states that the round function is not predictable when the rounding digit is 5. To get around this problem you can use the following instead:

y = int(x + 0.5)

This should give you predictable behaviour because int will always round down.

Points to note:

1) int returns a double so conversion might be needed (see
 bruintje's wrapper suggestion)
2) Consider using 'fix' instead of 'int' depending on the behaviour required for negative numbers (see aikimark's link)

Avatar of Alon_h
Alon_h

Hi Sting79

Try this :


Dim i As Double

   i = 11.55

   If (i - Int(i)) > 0.5 Then
      MsgBox "A"
   Else
      MsgBox "B"
   End If