How can you round a number in a non-calculating field?

J

Jerye

I have a form field that needs to be rounded, but it is not a calculating
form field, it is the field used in the calculation of another form field. Is
there a way to do this?

Jerye
 
S

Stefan Blom

As far as I know, you cannot actually round numbers in a display field, but
you can specify a number format so that Word drops digits that you don't
want to show (for example, all decimals but the first two).
 
M

macropod

Hi Jerye,

You can make a formfield round a number to various levels of precision. If you set the formfield's 'type' property to 'number' you
then get the option to set the precision. That's the precision level the formfield will store, regardless of what's input. If you
need to use a rounded version of that in another formula, you could use a formula field coded as:
.. {=ROUND(Text1,-2)} to round the text formfield's value off to the nearest 100
.. {=ROUND(Text1,0)} to round the text formfield's value off to the nearest whole number
.. {=ROUND(Text1,2)} to round the text formfield's value off to the nearest 1/100
etc, where 'Text1' is the formfield's bookmark name.
 
S

Suzanne S. Barnhill

I believe it's been established that Word does actually round numbers when
you set a specific number of digits to display.

--
Suzanne S. Barnhill
Microsoft MVP (Word)
Words into Type
Fairhope, Alabama USA
http://word.mvps.org
 
S

Stefan Blom

When I test this (text form field of type "Number," with the format set to
"0.00,"), Word doesn't seem to round the number correctly, if I add
excessive decimals.
 
J

Jerye

What I am needing is to be able type in 14.9 and it will round to 15 or 14.3
and it will round to 14. Word always rounds down, no matter what. This is not
a calulating form field, so I can't put in a formula.

Jerye
 
M

macropod

Hi jerye,

You can only achieve what you're after with vba. You could for example, use code like:
Option Explicit
Dim FfName As String

Sub GetFF()
FfName = Selection.Bookmarks(1).Name
End Sub

Sub RoundIt()
ActiveDocument.FormFields(FfName).Result = Round(ActiveDocument.FormFields(FfName).Result, 0)
End Sub
in a normal code module and make 'GetFF' the 'On Entry' macro and 'RoundIt' the 'On Exit' for the formfield, which must also have a
bookmark name.

If the formfield is formatted as text, you'll get a result equal to the rounded number, with no decimals. If the formfield is
formatted as number, you'll need to have at least one decimal, and the returned result will be the whole number plus '.0' (or
however many decimals you're using).

Note: If the formfield you're dealing with is the first in the document, you'll also need to use code like the following in the
document's 'This Document' module:

Option Explicit

Private Sub Document_Open()
Call GetFF
End Sub
 

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