Discussion Locked

Rounding decimal to nearest tenth place

0 helpful
11 replies
Closed to new repliesThis discussion is closed. It stays readable, but it takes no new replies.
Rounding decimal to nearest tenth place in ACCESS 95. On the form the field is a calculated value(currency field). But if go into that text box it shows a number for example $1.2925555 instead of $1.29. I tried decimal places but it doesn't work it's already set to 2.

11 replies

by dklippert

What happens when you change the Format to $0.0 and also choose 1 for decimal places? What are the formats for the calculation components?

by true_texan

I'm not sure how the formatting in Access 95 works, but there's always the mathematical aproach. Multiply the number by 10; take the integer of the number; and divide by 10. If n is your number, the formula would look something like:

n = INT(n*10)/10

The number may still display in currency (ie. $0.00), but the number would be rounded to the nearest 10th.

Hope this helps!

by c1980

A label would of course show what you want. But a text box, once it gets focus, shows the true value of the field. In order to display this as you want when the text boy gets the focus you need to set the data input/mask format of the text box to:"#,0.00", or whatever you want.

by bob sellman

The mathematical approach is the correct approach, but you've got to use the correct formula so that you round and you get to the right number of decimal places:

value = .01 * INT(.5 + (100 * value))

The .5 can be .49 or whatever suits your needs best for rounding. .5 will round up anything where the third decimal place in value is .5 or higher.

To get this to work, you should include the above code (substituting the name of the control, let's say txtValue, for value), as part of theonUpdate property of the control txtValue. So it would read:

me.txtValue = .01 * INT(.5 + (100 * me.txtValue))

Hope this helps.

by sl-campbell

A text box, when it gets the focus, will always show the true value assigned to it. This is normal, and it should be this way...
However, if you want it to show a different value, which is the apparently the case here, then you need to set the value of the text box in code. If you go to the code window, and select the Form_Current event and here do your calculations with a variable and THEN assign the results to the text box, formating it at the same time, you will get want you want because the text box is now being assigned to a variable that holds the rounded value instead of being assigned to a un-rounded value and THEN being rounded or formated.

Private Sub Form_Current()
Dim dCalc As Double

'doing some calculation each time the recordset is repositioned
dCalc = 123456789.234569

'Round and format the calculated value to 2 decimal places
Text1.Value = Format$(dCalc, "#,0.00")

End Sub

If the text box should hold the correct value, unformated, but the user should see the rounded value when the text box gets the focus, then you need to use the Input Mask of the text box as decsribed in answer #3.

This discussion is closed to new replies.