Formating # of decimal places displayed in Textbox

B

BRC

using access 2003 and Win xp pro
I have a problem with formatting a form textbox to display the the
correct number of decimals. The text box is a calculated field that
is [grams_unit] * [quanity]. Grams_unit is in a table as field size:
"single", format: "general number", decimal places: "2". In the table
the grams_unit displays as .85. However when I do the calculation for
my textbox (totalgramsTB) 1*grams_unit yields 0.850000023841858 and
it displays 15 decimal places. What I would like is for it to display
only .85. I have no idea where all these numbers are coming from. I
don't want to set the format to fixed because I don't want it to
appear to be currency. Any suggestions would be appreciated. As
always, thanks for any help
BRC
 
A

Allen Browne

Okay, this is typical of floating point calculations.

You may be able to help the display by explicitly rounding, i.e. change the
calculation from:
[grams_unit] * [quanity]
to:
Round([grams_unit] * [quanity], 2)

However, the floating point error will still be present. An alternative
approach would be to use a fixed point number, such as Currency. By forcing
the calcuation results to this type, you avoid the floating point problems.
By default, Access will show the result as dollars and cents (or whatever
your currency is), butyou can circumvent that by setting the Format property
of your text box to Fixed. The calcuation would be:
CCur(Nz(Round([grams_unit] * [quanity], 2),0))

For info on why floating point numbers cannot accurately represent real
numbers, see:
http://en.wikipedia.org/wiki/Floating_point#Accuracy_problems
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top