Solved

Rounding

Posted on 2002-07-19
7
248 Views
Last Modified: 2010-05-02
i need to be able to round down and round up (a number of course). any know how?
0
Comment
Question by:CyberAccess
7 Comments
 
LVL 43

Expert Comment

by:TimCottee
ID: 7164670
Use the Round() function!

Debug.Print Round(3.3)
Debug.Print Round(3.6)
Debug.Print Round(3.678,2)
0
 
LVL 4

Expert Comment

by:gencross
ID: 7164671
Sounds too easy :)
0
 
LVL 44

Expert Comment

by:Arthur_Wood
ID: 7164685
be aware the the Round function uses "Banker's Rounding":

If a Value is EXACTLY .5, then if the rounding will be to the EVEN whole Number - 2.5 will ROUND Down to 2, 3.5 will ROUND Up to 4.

Arthur Wood
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 69

Expert Comment

by:Éric Moreau
ID: 7164686
One thing for sure, do not use the Format function to round numbers. To know more on this subject, have a look at http://www.experts-exchange.com/jsp/qManageQuestion.jsp?ta=visualbasic&qid=20311047
0
 
LVL 7

Expert Comment

by:Joe_Griffith
ID: 7164813
Be aware that the VB Round function, while more accurate, is not acceptable by the IRS and many businesses will complain about it.  Here's the function I use... it's crude but effective:

Public Function fc_spRound(ac_Amount As Currency, al_Places As ppRound) As Currency
  If ac_Amount < 0 Then
    Select Case al_Places
   
    Case ppOneCent
      fc_spRound = Fix((ac_Amount - 0.005) * 100) / 100
   
    Case ppFiveCents
      fc_spRound = Fix((ac_Amount - 0.025) * 20) / 20
     
    Case ppDollar
      fc_spRound = Fix(ac_Amount - 0.5)
     
    Case ppTruncateToCents
      fc_spRound = Fix(ac_Amount * 100) / 100
   
    Case ppTruncateToDollars
      fc_spRound = Fix(ac_Amount)
     
    End Select
 
  Else
    Select Case al_Places
   
    Case ppOneCent
      fc_spRound = Fix((ac_Amount + 0.005) * 100) / 100
   
    Case ppFiveCents
      fc_spRound = Fix((ac_Amount + 0.025) * 20) / 20
     
    Case ppDollar
      fc_spRound = Fix(ac_Amount + 0.5)
     
    Case ppTruncateToCents
      fc_spRound = Fix(ac_Amount * 100) / 100
   
    Case ppTruncateToDollars
      fc_spRound = Fix(ac_Amount)
     
    End Select
  End If
End Function
0
 
LVL 7

Expert Comment

by:Joe_Griffith
ID: 7164826
Here is a link to more than you ever wanted to know about rounding in VB:

http://support.microsoft.com/default.aspx?scid=kb;en-us;Q196652&SD=MSKB&
0
 
LVL 2

Accepted Solution

by:
mbenassor earned 20 total points
ID: 7169279
I also crossed this problem, you can use my function :

x : is the number you want to apply round
n : number of digits to keep

Private Function NewRound(x As Double, n As Integer) As Double
   Dim factor As Double, y As Double
   
   factor = 10 ^ n
   y = x * factor + Sgn(x) * 0.5
   NewRound = Fix(y) / factor
End Function
0

Featured Post

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Suggested Solutions

When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
If you have ever used Microsoft Word then you know that it has a good spell checker and it may have occurred to you that the ability to check spelling might be a nice piece of functionality to add to certain applications of yours. Well the code that…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…

708 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

14 Experts available now in Live!

Get 1:1 Help Now