Format Percentages and Other Decimal Numbers

From HotDocs Wiki

Jump to: navigation, search

There are several ways you can format percentages and other decimal numbers in your templates. For example, decimal numbers can be merged in the template using digits (5.49) or words (five point forty-nine). When formatting decimal numbers, your results depend on several factors. First of all, you must indicate the number of decimal places at the Number Variable Editor. Since numbers are rounded to this number of decimal places, make sure it is large enough to allow users to enter the appropriate number of decimal places for the given situation.

You can also help users enter appropriate answers by assigning a currency symbol to the variable. For example, you can use the $ currency symbol for dollar amounts. This also works for percentages; enter % as the currency symbol and it will appear next to the variable field in the interview, just like other currency symbols. Setting appropriate Minimum and Maximum values at the Number Variable Editor can also help prevent users from entering invalid answers, such as percentages less than zero, if applicable.

Once the user has entered an answer, you can use several formats to merge the answer in your template. For example, you can use two separate variable fields to merge the answer in the template: one field using the nine format to insert the number as a word, and one field without a format to insert the number using digits:

    «Percentage:nine» percent («Percentage»%)

The nine format works well for whole numbers (thirty-five), but not numbers with decimal places. Depending on your preferences, another format you can use is nine point nine, which would format 5.49 as five point forty-nine and 5.50 as five point fifty. However, if you want 5.49 to be spelled out as five point four nine, you must use a Computation variable instead of a regular number format.

The following computation formats a decimal number such as 9.99 as nine point nine nine instead of nine point ninety-nine. The computation first formats the number before the decimal point using the zero format. (This format will spell the number zero (0) as "zero." If you want it spelled as "no," use the nine format. If you want it spelled as "none" use the none format.) If the number contains decimal places, the word point is added to the result, followed by the digits after the decimal point formatted individually using the zero format.

NOTE:

There are two example scripts below. The first script will work for any number of decimal places, but it uses the WHILE instruction, which works only in HotDocs 6.2 and higher. The second example works in all versions of HotDocs, but it handles only up to three decimal places. (You can modify it to handle more, however.)

Example

//Example 1: This works for any number of decimal places. (Requires HotDocs 6.2 or higher.) 
"" 
FORMAT( TRUNCATE( Percentage, 0), "zero" ) 
IF Percentage != TRUNCATE( Percentage, 0 ) 
  RESULT + " point" 
  SET Num Var TO POSITION( "«Percentage»", "." ) + 1 
  WHILE Num Var <= LENGTH( "«Percentage»" ) 
    RESULT + " " + FORMAT( INTEGER( MID( "«Percentage»", Num Var, 1 ) ), "zero" ) 
    INCREMENT Num Var 
  END WHILE 
END IF 
RESULT + " percent" 
 
//Example 2: Each decimal place must be handled separately. This version handles up to three decimal places. 
"" 
FORMAT( TRUNCATE( Percentage, 0), "zero" ) 
IF Percentage != TRUNCATE( Percentage, 0 ) 
  RESULT + " point" 
  SET Num Var TO POSITION( "«Percentage»", "." ) + 1 
  RESULT + " " + FORMAT( INTEGER( MID( "«Percentage»", Num Var, 1 ) ), "zero" ) 
  SET Num Var TO Num Var + 1 
  IF Num Var <= LENGTH( "«Percentage»" ) 
    RESULT + " " + FORMAT( INTEGER( MID( "«Percentage»", Num Var, 1 ) ), "zero" ) 
  END IF 
  SET Num Var TO Num Var + 1 
  IF Num Var <= LENGTH( "«Percentage»" ) 
    RESULT + " " + FORMAT( INTEGER( MID( "«Percentage»", Num Var, 1 ) ), "zero" ) 
  END IF 
END IF 
RESULT + " percent"

Percentage Any Number variable with one or more decimal places.
Num Var A temporary Number variable used in the WHILE loop. (See Create a Temporary Variable.)