Retro video games delivered to your door every month!
Click above to get retro games delivered to your door ever month!
X-Hacker.org- Turbo Pascal - <b> form numeric and string formatting pp 294</b> http://www.X-Hacker.org [<<Previous Entry] [^^Up^^] [Next Entry>>] [Menu] [About The Guide]
 FORM                   Numeric and String Formatting                 pp 294

 Syntax:  Form (FormatString,Var1,Var2,..VarN)

 Type:    String

 Form:    Function

 Purpose: Format output to conform to the image of FormatString.

 Notes:    FORM is used with TURBOBCD compiler only.

          FormatString is a string expression giving an image of the desired
          format of the data.  The resulting output is a string the same
          length as FormatString.

          FormatString is made up of a number of field specifiers, each of
          which corresponds to one parameter in the parameter list.  Blanks
          and non defined characters separate the fields.


 NUMERIC DATA:

 # digit:   # indicates a digit position.  If the field specifier contains no
          @ or * characters then unused digits are returned as blanks.  If
          the specifier is without a sign position (+-) a floating minus sign
          will be returned in front of a negative number.
          A number greater than the field specifier will be returned as all *.

 Example: Form ('####',34.567)              =     '  35'
          Form ('###.##',12.345)            =     ' 12.35'
          Form ('####.##',-12.3)            =     ' -12.30'
          Form ('###.##,1234,56)            =     '***.**'  { overflow }

          Real1  := 1234567.890123456
          Real2  := 1.1
          StrVar := Form ('############.##',RealVar); { '     1234567.89' }
          WriteLn (Real1:7:2);                        { '1234567.89' }
          WriteLn (Real2:7:2);                        { '      1.10' }


 @ digit:   @ indicates a digit position.  If the field specifier contains
          one or more of these, then all blanks are returned as leading zeros.
          The sign will not be returned unless the + symbol is present.

 Example: Form ('@###',34.567)              =     '0035'
          Form ('###.#@',12.345)            =     '012.35'




 * digit:   * indicates a digit position.  If the field specifier contains
          one or more of these, blanks are returned as leading asterisks.

 Example: Form ('*###',34.567)              =     '**35'
          Form ('###.#*',12.345)            =     '*12.35'





 $ digit:   $ indicates a digit position.  If the field specifier contains
          one of these, a floating $ sign is returned in front of the number.

 Example: Form ('$#####.##',123.45)         =  '  $123.45'
          Form ('######.#$',-12.345)        =  '  -$12.35'
          Form ('*$####.##',12.34)          =  '***$12.34'




 - digit:   - indicates a sign position.  If the field specifier contains
          one of these, a minus will be returned for a negative number.

 Example: Form ('-###.##',-1.2)             =  '-  1.20'
          Form ('-###.#$',12)               =  '  12.00'
          Form ('*#####.##-',-123.45)       =  '***123.45-'




 + digit:   + indicates a sign position.  If the field specifier contains
          one of these, a minus will be returned for a negative number and a
          plus sign will be returned for a postive number.

 Example: Form ('+###.##',-1.2)             =  '-  1.20'
          Form ('+###.#$',12)               =  '+ 12.00'
          Form ('*#####.##+',-123.45)       =  '***123.45_'


 , digit:   , indicates a decimal comma or a separator comma.  The last
          period or comma in the numeric image is the decimal delimiter.

 . digit:   . indicates a decimal period or a separator period.  The last
          period or comma in the numeric image is the decimal delimiter.

 Example: Form ('##,###,###.##', 12345.6)   =  '    12,345.60'
          Form ('$#,###,###.##',-12345.6)   =  '  -$12,345.60'
          Form ('*$,###,###.##+',12345.6)   =  '***$12.345.60+'
          Form ('##,###.##',     123456.0)  =  '**,***.**'

 STRING DATA:

 # digit:   If the string image contains only # characters then the
          string will be left justified.

 @ digit:   If the string image contains any @ characters then the
          string will be right justified.

          Note that if the string is longer than the string image, the
          string will be left justified and truncated to the length of the
          image.

 Example: Form ('##########', 'Pascal')     =  'Pascal    '
          Form ('@#########', 'Pascal')     =  '    Pascal'
          Form ('#####','TURBO Pascal')     =  'TURBO'  { Overflow }
          Form ('@@@@@','TURBO Pascal')     =  'TURBO'  { Overflow }




 Usage:
        CONST
          Real1   : Real       = 12345.67  ;
          String1 : String [5] = 'ABCDE'   ;

       VAR
          String2 : String [20]            ;
          Fil     : Text                   ;

       BEGIN
          String2 :=   Form ('##########.##', Real1);    { '     12345.67'  }
          WriteLn     (Form ('##########.##', Real1));   { '     12345.67'  }
          WriteLn     (Real1:13:2);                      { '     12345.67'  }
          WriteLn     (Form ('##,###,###.##', Real1));   { '    12,345.67'  }
          WriteLn     (Form ('##,###,###.###',Real1));   { '    12,345.670' }
          WriteLn (Fil,Form ('##########.##', Real1));   { '     12345.67'  }
          WriteLn     (Form ('@############', String1)); { '        ABCDE'  }
          WriteLn     (Form ('#############', String1)); { 'ABCDE        '  }
          WriteLn (Fil,Form ('@############', String1)); { '     12345.67'  }
       END.

See Also: Write WriteLn

Online resources provided by: http://www.X-Hacker.org --- NG 2 HTML conversion by Dave Pearson