Round() - Can I round DOWN? (1 Viewer)

voidcranium

Registered Something.
Local time
Today, 12:05
Joined
Oct 29, 2006
Messages
175
I need to have a text box show a number that is rounded down.
Here is the code I have.

This code rounds UP I need it to round DOWN.
Code:
Private Sub cmdCalculate_Click()
txtTotalPacks = Round((txtSheetsRan * [# Up]) / [# in Pack])

This
((2750*2)/1000) = 5.5
needs to be this
((2750*2)/1000) = 5

I want this rounded down so it is 5
We do not ship uneven cartons.

Thanks for any help.

the VOID
 

pbaldy

Wino Moderator
Staff member
Local time
Today, 10:05
Joined
Aug 30, 2003
Messages
36,118
Rather than round, you could use the Int() function to grab the integer value of the result.
 

EdFred

knows enough, dangerous
Local time
Today, 13:05
Joined
Mar 2, 2007
Messages
129
Rather than round, you could use the Int() function to grab the integer value of the result.


Depending on the numbers Fix() may be a better option.

From the 2007 Help File:

MyNumber = Int(99.8) ' Returns 99.
MyNumber = Fix(99.2) ' Returns 99.

MyNumber = Int(-99.8) ' Returns -100.
MyNumber = Fix(-99.8) ' Returns -99.

MyNumber = Int(-99.2) ' Returns -100.
MyNumber = Fix(-99.2) ' Returns -99.

I know, not for this particular instance, but it may turn up in a search function, if anyone uses that on here anymore. ;)
 
Last edited:

pbaldy

Wino Moderator
Staff member
Local time
Today, 10:05
Joined
Aug 30, 2003
Messages
36,118
You're right; if they ship out negative quantities of cartons.
 

EdFred

knows enough, dangerous
Local time
Today, 13:05
Joined
Mar 2, 2007
Messages
129
You're right; if they ship out negative quantities of cartons.

Well, sometimes we "bill" ourselves for a negative amount when I magically find material in inventory. We also ship a negative amount if a customer sends material back.
 

voidcranium

Registered Something.
Local time
Today, 12:05
Joined
Oct 29, 2006
Messages
175
Thanks guys.
I used the int() and it worked.
 

19rallen60

New member
Local time
Today, 13:05
Joined
Sep 11, 2019
Messages
1
How about Microsoft and its cluster of goofballs simply provide RoundUp and RoundDown functions that can be used in a query. SQL server provides a method to do it, so should Access.


I would think the syntax should be:
RoundDown([value],[number of decimal places])

Example1: =RoundDown([MyCost],1). This means values like 2.18, 2.199 or 2.1004 would become 2.1 regardless of the number of decimal places the original value is expressed in.


Example2: =RoundDown([MyCost],0). This means values like 2.18, 2.199 or 2.1004 would become 2.0 regardless of the number of decimal places the original value is expressed in.

Same syntax method for RoundUp
 
Last edited:

theDBguy

I’m here to help
Staff member
Local time
Today, 10:05
Joined
Oct 29, 2018
Messages
21,358
How about Microsoft and its cluster of goofballs simply provide RoundUp and RoundDown functions that can be used in a query. SQL server provides a method to do it, so should Access.
Hi. Welcome to AWF! But that would make too much sense. :)
 

Galaxiom

Super Moderator
Staff member
Local time
Tomorrow, 04:05
Joined
Jan 20, 2009
Messages
12,849
Care should be taken when rounding down the results of floating point calculations.

When using Int() or Fix() to round down (depending on your definition of "down" in the negative realm), we must be allow for the floating point errors in calculations that aught to be integers but come out at tiny margins below the true result.

There are a few ways to deal with this. We can add or subtract a small buffer which will push the floating point over the integer threshold that it should have been. Or process the value having multiplied it by some orders of magnitude and rounding before performing the calculation and then dividing back down.

But these are fudges. Where precise arithmetic is required, use Decimal or Currency datatypes. These are scaled integers so all calculations are done at higher orders of precision.

Decimal allows the Precision (number of significant digits) and Scale (the number of decimal places) to be set by the user. Currency is a Decimal datatype with the Scale fixed to four decimal places.
 

apr pillai

AWF VIP
Local time
Today, 22:35
Joined
Jan 20, 2005
Messages
735
The ROUNDDOWN() Access Function Code:
Code:
Public Function ROUNDDOWN(ByVal N As Double, ByVal p As Integer) As Double
Dim S1 As Integer, S2 As Integer

S1 = Sgn(N)
S2 = Sgn(p)
Select Case S1
    Case 1
        Select Case S2
            Case 1
                ROUNDDOWN = (Int(N * (10 ^ p)) / 10 ^ p) * S1
            Case -1
                ROUNDDOWN = Int(N / (10 ^ Abs(p))) * 10 ^ (Abs(p) * Abs(S2))
        End Select
    Case -1
        Select Case S2
            Case 1
                ROUNDDOWN = (Int(Abs(N) * (10 ^ p)) / 10 ^ p) * S1
        End Select
End Select
End Function
 

Users who are viewing this thread

Top Bottom