Wrox Programmer Forums
|
Beginning VB 6 For coders who are new to Visual Basic, working in VB version 6 (not .NET).
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Beginning VB 6 section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old November 6th, 2006, 01:00 PM
Authorized User
 
Join Date: Oct 2006
Posts: 55
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to ricespn
Default Rounding Numbers

Hi,

How can i round a decimal number to the next number?

Example:

2.45 = 3
.01 = 1
34.01 = 35


Thanks !


=======================
Strange and crazy, but everything is possible
__________________
=======================
Strange and crazy, but everything is possible
 
Old November 6th, 2006, 03:19 PM
Friend of Wrox
 
Join Date: May 2006
Posts: 643
Thanks: 0
Thanked 0 Times in 0 Posts
Default

A complex subject.
There are numerous ways to approach rounding: You have to take into consideration such things as Bankers Rounding, and the various default roundings applied by intrinsic vb functions.

Here is a little code that hopefully does what you wnnt:
Code:
Private Sub Command1_Click()


    Dim dblPlaces As Double
    Dim dblAmount As Double
    Dim dblRounded As Double

    dblAmount = 34.01

    ' dblPlaces is the decimal place you want to round to:
    '    1 = no decimal places
    '    10 = 1 decimal place
    '    100 = 2 decmial places... and so on
    dblPlaces = 1

    dblRounded = Fix(dblAmount * dblPlaces) / dblPlaces
    ' The following needs to be done for positive amounts, but not
    '   for negative amounts for true "rounding up" functionality
    '   so you would need to add a test for negative amounts and NOT
    '   do this for negatives.
    dblRounded = dblRounded + (1 / dblPlaces)  ' See note below

    Debug.Print dblRounded

End Sub
Note the line of code that moves the value up for positive numbers. You don't do this for the negative numbers as you'll move one value unit too far. Also remember that rounding up for negative numbers moves you towards zero. If you always want to move "away" from zero, then you have to apply the ABS function to your calculations and then re-apply the sign after the rounding operation is complete.

Make sense?

Have fun...

Woody Z http://www.learntoprogramnow.com
 
Old November 6th, 2006, 03:33 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 627
Thanks: 0
Thanked 0 Times in 0 Posts
Default

I guess the number 1.0 should remain 1.0 and not 2.0, right?
Code non tested:

public function Ceil(byval n as double) as double
if n-fix(n) = 0 then
ceil= n '' if the decimal part is zero, no change
else
ceil = fix(n)+1 '' add 1 to the inetger part
end function
 
Old November 6th, 2006, 05:00 PM
Authorized User
 
Join Date: Oct 2006
Posts: 55
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to ricespn
Default

Thanks guys!

But:

What about this one:

Private Sub Command1_Click()

    Dim dblAmount As Double

    dblAmount = .1
    Var = Int(dblAmount)
    If Var = 0 Then
    Var = Var + 1
    End If

   Debug.Print Var

End Sub

=======================
Strange and crazy, but everything is possible
 
Old November 6th, 2006, 05:19 PM
Friend of Wrox
 
Join Date: May 2006
Posts: 643
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Quote:
quote:Originally posted by ricespn
 Thanks guys!

But:

What about this one:

Private Sub Command1_Click()

    Dim dblAmount As Double

    dblAmount = .1
    Var = Int(dblAmount)
    If Var = 0 Then
    Var = Var + 1
    End If

Debug.Print Var

End Sub
I don't think that will do what you want, which is rounding up. In your code, you round up ONLY if the value is zero after rounding - which has several problems with it, including that if your value was zero to start with, it would result in a value of one!!!

Take another look at the fix I suggested. Or better still, take a look at http://www.xbeat.net/vbspeed/c_Round.htm.

I have written a great deal of rounding algorithms since I've done a lot of work in the financial arena - and there is a lot to understand about how all this works if you need dependable results.


Woody Z http://www.learntoprogramnow.com
 
Old November 6th, 2006, 05:24 PM
Authorized User
 
Join Date: Oct 2006
Posts: 55
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to ricespn
Default

You are right! I found that out after I posted it

=======================
Strange and crazy, but everything is possible





Similar Threads
Thread Thread Starter Forum Replies Last Post
Rounding Numbers and Displaying Decimals elygp Crystal Reports 2 May 8th, 2007 10:56 AM
Rounding JasperGIS Beginning VB 6 1 December 14th, 2005 03:09 PM
Rounding Numbers ashley_y VB How-To 2 February 3rd, 2004 06:06 AM
What's with the rounding?? kaizer BOOK: Beginning Java 2 2 December 22nd, 2003 11:36 PM
Rounding in C# cjo ASP.NET 1.0 and 1.1 Basics 3 November 3rd, 2003 04:12 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.