Format Numeric Field Results

Format Numeric Field Results

figs/moderate.gif figs/hack73.gif

How you display the result of a calculation can be almost as important as the result itself. This hack shows you how to get field results into the format you need.

If you use fields to calculate numbers, such as the sum of the values in a table column [Hack #72], you'll usually want some control over how those results are formatted. For example, you might want the number rounded to two decimal points with a currency symbol. To control how numbers appear in fields, you use numeric picture switches.

A numeric picture switch is indicated in a field's code with \# and can include a variety of instructions to Word on how to format the numeric results of a particular field.

For example, insert the following field in a document (to create field braces, you must press Ctrl-F9 or select InsertField):

{=2+2 \# 00.0000}

Select the field and press F9. The numeric picture switch tells Word to display the results of this sum in the form 04.0000.

If you use the switch \# 0, Word rounds the result to the nearest integer. The following field would display the value 3 in a document:

{=3.1415 \# 0}

If you omit the numeric picture switch, Word makes its own decisions as to whether to display a calculation's result as an integer or round the number to one or two decimal places.

You can also include a currency symbol in a numeric picture switch. The following field displays $82.37 in your document:

{=50 + 32.37 \# $00.00}

1 Compound Picture Switches

Numeric picture switches actually take three arguments, separated by semicolons: positive value format, negative value format, and zero value format. If you omit these arguments, as in the previous examples, the switch uses the positive value format to format the results. If you want to format negative values differently, you can add a second parameter to the switch. For example, the switch \# #;(#) used in a field would tell

If no value is given for one of the three arguments (positive, negative, and zero), Word won't display those results. For example, the switch \# #;; displays the results only if they're greater than zero.

You could also use a switch like \# #;-#;Ø to display Ø for zero values (hold down the Alt key and type 0216 on the number pad to get the Ø character). You can also display different text outputs for positive and negative values. For example, the following switch:

\# Profit\ $,0.00;Loss\ $,0.00;Break\ Even

prefaces positive values with the word Profit and negative values with the word Loss. If the value is zero, it displays only the term Break Even, with no number. Notice that a backslash is placed in front of a space to tell Word to display the space in the field results. A significant benefit of adding text and/or suppressing zeros this way (instead of using IF tests to output nulls or spaces) is that the field will continue to evaluate as a number in other formulas.

In addition, the field results reflect any font formatting applied to the numeric picture switch. For example, in the above switch, you could color the Profit portion of the switch blue, the Loss portion red, and the Break Even portion green.

Paul Edstein

     Python   SQL   Java   php   Perl 
     game development   web development   internet   *nix   graphics   hardware 
     telecommunications   C++ 
     Flash   Active Directory   Windows