Need code to roundup a calculation in vba

Punice

Registered User.
Local time
Today, 09:36
Joined
May 10, 2010
Messages
135
In Access 2007, I dug myself into a 'coding hole'. I have this:
Me.[SUTA_Total] = DSum("([SUTA_Tax])", "tblWages")
which calculates a value of "13.5989" when certain conditions are
met from executing the code prior to this statement in a sub-routine.

I need to know, exactly, what code to use that will round that value to "13.60".

I know that I can do it in a query, but do it there creates an error reporting that the code in the sub-routine can't find the [SUTA_Total] field.

Being lazy, this time, and asking for help relatively early.:D
 
Have you tried: Round(DSum("([SUTA_Tax])", "tblWages"),2)
 
you said round UP

the round function will round sensibly.

1.4149 rounds down to 1.41
1.4151 rounds up to 1.42
1.4150 rounds to nearest whole number, in this case 1.42

if you ALWAYS want round UP, you need a slightly different technique,
 
1.4150 rounds to nearest whole number, in this case 1.42

Although the result in this specific case is correct the reason given isn't. When the number is equidistant from the nearest numbers as specified in the decimal places parameter, Round() will round to the nearest even digit.

1.425 rounds to 1.42
1.435 rounds to 1.44
 
thanks for tidying it up, @G. I meant even number, not whole number.
 
Have you tried: Round(DSum("([SUTA_Tax])", "tblWages"),2)


Yes, I did try that...didn't work.

I only need to roundup, because the number that needs to be rounded up will always be lower than "13.60" before the rounding portion of the sub-routine is required.

Oops! I did not try what you suggested, initially. When I did, I got a compile error saying 'Expected ='.
 
Last edited:
the easiest way to ALWAYS round up is to use the INT function

int([taxvalue]*100) rounds the tax value down - int(12.4356)*100 = 1243

int([taxvalue]*100)/100 gives you the rounded down value to 2 DPs. = 12.43

if you want to always round up, you need to use negatives, and then negate the result

-int(-[taxvalue]*100)/100 - in the above example,. you get 12.44
 
the easiest way to ALWAYS round up is to use the INT function

There is a gotcha with using Int() for rounding due to the imprecise nature of storing floating point decimals as Single or Double in a base 2 system. Sometimes the result of a calculation that aught to return an integer will return a number slightly over or under. The rounding will be out by 1.

For example, try this calculation:

Code:
Int(0.7 * 90)
 
interesting example.

I don't think this would give a problem in the usage I quoted, especially if the OP is using currency rather than fp data types.
 

Users who are viewing this thread

Back
Top Bottom