Solved

How to round down in VB?

Posted on 2001-06-25
10
198 Views
Last Modified: 2010-05-02
What is the best way to round down a number?
0
Comment
Question by:joesmow
  • 3
  • 2
  • 2
  • +3
10 Comments
 
LVL 50

Expert Comment

by:Ryan Chong
ID: 6226031
do while Conditon
   i = i - j
loop
0
 
LVL 28

Accepted Solution

by:
AzraSound earned 50 total points
ID: 6226046
Int(number) will return just the integer portion...
or
Fix(number)

they differ in how they round negative numbers (if thats an issue for you)
0
 
LVL 50

Expert Comment

by:Ryan Chong
ID: 6226057
Oops..
0
Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

 
LVL 27

Expert Comment

by:Ark
ID: 6226172
Debug.Print Format(5459.455, "##,##0.00")

For more information place cursor on 'Format' word and press F1.

Cheers
0
 
LVL 5

Expert Comment

by:nilapenn
ID: 6226258
use the found function

for (eg)

round(45.34,2)=45
round(45.55)=46
roudn(45.55,1)=45.6
0
 
LVL 17

Expert Comment

by:inthedark
ID: 6227019
Warning Round has some strange bugs:

If you enter an immediate window and you type:
 
?round(0.5,0)

The .5 should round up to 1 but it produces a result of  0

Scratch head..............then try

?round(.95,1)

But in this case the .05 correctly rounds up.

I am not qualified to pass judgement on Microsoft's QA procedures.  The problems stem from how numerics are internally represented.

I would therefore suggest that you should not used either Round, Fix or Int. You should create your own function so that at some time in the future, if some smartypants find a problem with your rounding function you only have one place to change.

The following function will provide accurate results for financial applications.  Roundings for both negative and positive numbers are consistent. Which means that credit notes will always have the same total value as invoices.

Public Function Rounder(Value, DecimalPlaces As Integer)

Public Function Rounder(Value, DecimalPlaces As Integer)
If DecimalPlaces <= 4 Then
    Rounder = (Int(Abs(Value) * (10 ^ DecimalPlaces) + 0.50001) * (10 ^ -DecimalPlaces)) * Sgn(Value)
Else
    Rounder = (Int(Abs(Value) * (10 ^ DecimalPlaces) + (0.5 + (10 ^ (-DecimalPlaces - 2))))) * (10 ^ -DecimalPlaces) * Sgn(Value)
End If
End Function

Hope this helps, inthedark.
0
 
LVL 17

Expert Comment

by:inthedark
ID: 6227149
Warning Round has some strange bugs:

If you enter an immediate window and you type:
 
?round(0.5,0)

The .5 should round up to 1 but it produces a result of  0

Scratch head..............then try

?round(.95,1)

But in this case the .05 correctly rounds up.

I am not qualified to pass judgement on Microsoft's QA procedures.  The problems stem from how numerics are internally represented.

I would therefore suggest that you should not used either Round, Fix or Int. You should create your own function so that at some time in the future, if some smartypants find a problem with your rounding function you only have one place to change.

The following function will provide accurate results for financial applications.  Roundings for both negative and positive numbers are consistent. Which means that credit notes will always have the same total value as invoices.

Public Function Rounder(Value, DecimalPlaces As Integer)

Public Function Rounder(Value, DecimalPlaces As Integer)
If DecimalPlaces <= 4 Then
    Rounder = (Int(Abs(Value) * (10 ^ DecimalPlaces) + 0.50001) * (10 ^ -DecimalPlaces)) * Sgn(Value)
Else
    Rounder = (Int(Abs(Value) * (10 ^ DecimalPlaces) + (0.5 + (10 ^ (-DecimalPlaces - 2))))) * (10 ^ -DecimalPlaces) * Sgn(Value)
End If
End Function

Hope this helps, inthedark.
0
 
LVL 22

Expert Comment

by:rspahitz
ID: 6228292
But inthedark, you say, "I would ... suggest ... not [using]... Int"

then your code uses "int":

   Rounder = (Int(Abs(Value) * (10 ^ DecimalPlaces) + 0.50001) * (10 ^ -DecimalPlaces)) * Sgn(Value)

Granted, there is really no other practical way to truncate a value's integer.  You could convert to string, find the decimal, then remove it and convert back to numeric, but that seems like a silly way.

--
Basically, the functions you mention have problems with the decimal portion, so one way to deal with this is to "convert" to a larger scaled number:

Instead of int(123.5), you can try int((123.5*10-9)/10) which scales it to an integer, subtracts a rounding value (always in 9s), then scales back and removes the decimal portion.  I don't know if this has the same decimal-to-binary-to-decimal conversion problems, but they may be more limited.

Personally, I think it's overkill except for the most critical of applications, and I'd simply use Int(x).
0
 
LVL 17

Expert Comment

by:inthedark
ID: 6229529
rspahitz, you are quite right I used Int. I only posted my comment as I read that somebody else had suggested using the Round function. The points that I was trying to make: keep control - always use your own function for rounding so that you can future-proof your application against changes in Microsoft's current interpretation of rounding. And that I wouldn't like a VB newbie reading this to think that rounding was a simple subject.

AzraSound has already answered the question which was "What is the best way to round down a number?"
0
 
LVL 22

Expert Comment

by:rspahitz
ID: 6229537
Yes.  I think most of the other posts before mine addressed the issue, and you're quite right about the rounding issue.  And AzraSound has the simplest (and first) solution that will probably work.
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
Reading the Contents of a Directory In Access VBA 5 68
Best way to parse out a json string in VB6? 10 162
vb6 connector to SQL Server 2 37
Advice in Xamarin 21 78
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…
When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
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…

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