Solved

control the decimal of an integer no.

Posted on 2002-03-21
7
182 Views
Last Modified: 2010-05-02
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  
0
Comment
Question by:Sting79
7 Comments
 
LVL 44

Expert Comment

by:bruintje
ID: 6884933
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
0
 
LVL 44

Accepted Solution

by:
bruintje earned 50 total points
ID: 6884948
seems like int IS working because i can't reporduce the bug so use int(value) to round up or down
0
 
LVL 44

Expert Comment

by:bruintje
ID: 6884954
should test it better ROUND will do what you want
0
Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

 
LVL 9

Expert Comment

by:Valliappan AN
ID: 6884981
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.
0
 
LVL 45

Expert Comment

by:aikimark
ID: 6884986
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.
0
 

Expert Comment

by:DJWalker
ID: 6885052
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)

0
 

Expert Comment

by:Alon_h
ID: 6885164
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


0

Featured Post

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
consolidate or create a new vba code for performing tasks 5 65
Copy a row 12 61
Crystal reports - Formula Field code need assistance with code 17 60
Excel Automation VBA 19 67
I’ve seen a number of people looking for examples of how to access web services from VB6.  I’ve been using a test harness I built in VB6 (using many resources I found online) that I use for small projects to work out how to communicate with web serv…
Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

816 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now