Using Variable Decimal Places in Report Writer

ID: Q99611

The information in this article applies to:

SUMMARY

By using report variables as described below, you can eliminate trailing zeros after the decimal instead of printing a fixed number of characters after the decimal.

MORE INFORMATION

The following steps create a report that always prints the first decimal place but does not print trailing zeros beyond the first decimal place:

1. Create a database with one numeric field called Number; length=10,

   decimals=4.

2. Add sample data to the database; some of the numbers should have zeros
   at the end; some should not. For example, 1.1000, 1.1200, 1.1230,
   1.1234.

3. Create a quick report based on this database.

4. Create the following report variables (all variables have an initial

   value of 0 [zero] and no calculation). The variables should appear in
   the variable list in the same order they are given below.

      Variable name  Value to store
      --------------------------------------------------------

      place4         IIF(VAL(RIGHT(STR(number,10,4),1))>0,1,0)
      place3         IIF(place4=1 OR
                        VAL(RIGHT(STR(number,10,3),1))>0,1,0)
      place2         IIF(place3=1 OR
                        VAL(RIGHT(STR(number,10,2),1))>0,1,0)
      numplaces      1+place2+place3+place4

5. Close the Report Variable dialog box and modify the report expression
   containing the numeric field so that is left-aligned and contains the
   following expression:

      STR(number,5+numplaces,numplaces).

   To left-align the field in FoxPro for MS-DOS:

    - Select the Style check box while in the Report Expression dialog box,
      then choose Left Alignment.

   To left-align the field in FoxPro for Windows:

    - Select the field on the report, then choose Alignment and Left from
      the Object menu.

   NOTE: "5+numplaces" along with left-aligning the field will ensure that
   the decimals are aligned. If this is not important, "5+numplaces" can be
   changed to "10" (without the quotation marks)--the full length of the
   field.

6. From the Report menu, choose Page Preview to see the output without
   trailing zeros beyond the first decimal place.

Additional reference words: FoxDos FoxWin 2.00 2.50 2.50a KBCategory: kbprint kbprg KBSubcategory: FxprintGeneral

Last Reviewed: June 26, 1995