Division Problem

  • Thread starter Thread starter Joe Delphi
  • Start date Start date
J

Joe Delphi

Greetings,

I have a requirement to divide two numbers and then round the result down to
the next whole number. In other words, if the result comes out to be 12.91,
the user wants to see 12.00. I know that rounding usually occurs at the .5
mark, but this is the way the user wants it so this is how it has to be.

I am attempting to do it like this:

..Fields("RawResult") = Fix(.Fields("Actual") / .Fields("Target") * 100) /
100

It works with some combinations of numbers, but not with others. For
example, when I enter 114 for Actual and 100 for target (114/100) I should
get a result of 1.14 but instead I get a result of 1.13. The fields are
all defined as numeric doubles.

Anyone know how to get it to round down to the next whole number without
giving what appears to be the wrong answer?


JD
 
Joe said:
Anyone know how to get it to round down to the next whole number without
giving what appears to be the wrong answer?

I think the problem with your formula may be that Access sometimes doesn't
keep the exact precision of the numbers involved - I think it's something to
do with the floating point or something. Anyway, the solution is actually
quite simple - there's a function that does exactly what you require ie.
returns the integer portion of a calculation. Try this:

..Fields("RawResult") = Int(.Fields("Actual") / .Fields("Target"))
 
First round the number to two places, then Fix.


fix( round(100* Actual/Target )/100 )

If the numbers are small enough, you will get a better
result with clng:

fix( clng(100* Actual/Target )/100 )

(david)
 
That's normal trucation and rounding errors since most numbers cannot be
represented exactly in computer since computer uses binary and not decimal
as we do.

The problem probably happens this way:

* (Real) 114 divided by (real) 100 probably gives 1.139999999...
* Multiply by 100 gives 113.9999999 ...
* Fix will simply trucates the decimal part and you get 113.
* When you divide 113 by 100, you get 1.13!

However, you numerical example doesn't tally with the description in the
first paragraph. If the user wants to see only the integral part of the
result, you simply use either Int() or Fix() function on the result (they
are different if result is negative) and not in the intermediate steps.
 
Back
Top