home *** CD-ROM | disk | FTP | other *** search
/ Programmer 7500 / MAX_PROGRAMMERS.iso / PASCAL / TOTDOC.ZIP / CHAPT14.TXT < prev    next >
Encoding:
Text File  |  1991-02-11  |  28.3 KB  |  757 lines

  1.                                                                           String
  2.                                                                         Handling
  3.  
  4.  
  5.  
  6.  
  7.  
  8.          "Guests, like fish, begin to smell after three days."
  9.  
  10.                                                                Benjamin Franklin
  11.  
  12.  
  13.  
  14.  
  15.  
  16.          The totSTR unit provides a set of routines for converting data to and
  17.          from string format. Although the unit includes the object FmtNumberOBJ,
  18.          the majority of the routines are good ol' functions.
  19.  
  20.  
  21.  
  22. String Functions
  23.  
  24.          These functions are grouped into the following categories reflecting
  25.          their primary purpose: string adjustment, string searching, word man-
  26.          agement and number conversion.
  27.  
  28.  
  29.  
  30. String Adjustment
  31.  
  32. Squeeze
  33.  
  34.          Squeeze(L:char;Str:string;Width:byte);
  35.  
  36.          Returns a string that has been squeezed (or expanded) to a specific
  37.          length. If the source string is too long, either the leftmost or right-
  38.          most characters are removed. The function is passed three parameters; a
  39.          character to indicate which side of the string to truncate ('L' or
  40.          'R'), the source string, and the desired string width. If the source
  41.          string is shorter than the desired width, the string is padded with
  42.          spaces. For example:
  43.  
  44.                   TIGHT :=SQUEEZE('L','ARKANSAS',6);
  45.  
  46.          Assigns the value 'KANSAS' to TIGHT.
  47.  
  48.  
  49.  
  50. PadLeft
  51.  
  52.          Padleft(Str:string;Size:byte;ChPad:char):string;
  53.  
  54.          Expands and left justifies a string to a specified width. The function
  55.          is passed three parameters; the source string, the length of the
  56.          expanded string, and the character used to pad the string. For example:
  57.  
  58.                   MyString := PadLeft('LOAD FILE',20,' ');
  59.  
  60.          Assigns the value 'LOAD FILE          ' to MyString.
  61.  
  62. 14-2                                                                User's Guide
  63.  
  64. --------------------------------------------------------------------------------
  65.  
  66. PadRight
  67.  
  68.          PadRight(Str:string;Size:byte;ChPad:char):string;
  69.  
  70.          Expands and right justifies a string to a specified width. The function
  71.          is passed three parameters; the source string, the length of the
  72.          expanded string, and the character used to pad the string. For example:
  73.  
  74.                   MyString := PadRight('RIGHT',10,'*');
  75.  
  76.          Assigns the value '*****RIGHT' to MyString.
  77.  
  78.  
  79.  
  80. PadCenter
  81.  
  82.          PadCenter(Str:string;Size:byte;ChPad:char):string;
  83.  
  84.          Expands and centers a string to a specified width. The function is
  85.          passed three parameters; the source string, the length of the expanded
  86.          string, and the character used to pad the string. For example:
  87.  
  88.                   Bored := PadCenter('Middle',14,'-');
  89.  
  90.          Assigns the value '----Middle----' to Bored.
  91.  
  92.  
  93.  
  94. Pad
  95.  
  96.          Pad(PadJust:tJust;Str:string;Size:byte;ChPad:char):string;
  97.  
  98.          This function combines the features of PadLeft, PadRight and PadCenter
  99.          into a single function. The totSTR unit includes the declaration of an
  100.          enumerated type tJust, with the following members: JustLeft, Just-
  101.          Center, and JustRight. The function is passed four parameters; a member
  102.          of tJust to indicate the required justification, the source string, the
  103.          length of the expanded string, and the character used to pad the
  104.          string. For example:
  105.  
  106.                   PadStr := Pad(JustCenter,'Middle',14,'-');
  107.  
  108.          Assigns the value '----Middle----' to PadStr.
  109.  
  110.  
  111.  
  112. SetUpper
  113.  
  114.          SetUpper(Str:string):string;
  115.  
  116.          Returns a string with all the alpha characters converted to upper case.
  117.          The string is passed one parameter; the source string. For example:
  118.  
  119.                   MyString := SetUpper('123abc456dEf');
  120.  
  121.          Assigns the value '123ABC456DEF' to MyString.
  122.  
  123. String Handling                                                             14-3
  124.  
  125. --------------------------------------------------------------------------------
  126.  
  127. SetLower
  128.  
  129.          SetLower(Str:string):string;
  130.  
  131.          Returns a string with the alpha characters converted to lower case. The
  132.          string is passed one parameter; the source string. For example:
  133.  
  134.                   MyString := SetLower('123ABC456DeF');
  135.  
  136.          Assigns the value '123abc456def' to MyString.
  137.  
  138.  
  139.  
  140. SetProper
  141.  
  142.          SetProper(Str:string):string;
  143.  
  144.          Returns a string with the first character of each word converted to
  145.          upper case. The string is passed one parameter; the source string. For
  146.          example:
  147.  
  148.                   MyString := SetProper('eeny meeny miny mo');
  149.  
  150.          Assigns the value 'Eeny Meeny Miny Mo' to MyString.
  151.  
  152.  
  153.  
  154. AdjCase
  155.  
  156.          AdjCase(NewCase:tCase; Str:string): string;
  157.  
  158.          This function combines the features of SetUpper, SetLower and SetProper
  159.          into a single function. The totSTR unit includes the declaration of an
  160.          enumerated type tCase, with the following members: Lower, Upper,
  161.          Proper, and Leave. If the member Leave is specified, the string will
  162.          not be adjusted. The function is passed two parameters; a member of
  163.          tCase to indicate the required capitalization, and the source string.
  164.          For example:
  165.  
  166.                   PStr := AdjCase(Proper,'tracy chapman');
  167.  
  168.          Assigns the value 'Tracy Chapman' to PStr.
  169.  
  170.  
  171.  
  172. Last
  173.  
  174.          Last(N:byte; Str:string):string;
  175.  
  176.          Returns the last part of a string. The function is passed two parame-
  177.          ters; the number of characters to extract, and the source string. For
  178.          example:
  179.  
  180.                   NewString := Last(11,'Never take drugs!');
  181.  
  182.          Assigns the value 'take drugs!' to NewString.
  183.  
  184. 14-4                                                                User's Guide
  185.  
  186. --------------------------------------------------------------------------------
  187.  
  188. First
  189.  
  190.          First(N:byte; Str:string):string;
  191.  
  192.          Returns the first part of a string. The function is passed two parame-
  193.          ters; the number of characters to extract, and the source string. For
  194.          example:
  195.  
  196.                   MyString := First(10,'I want sextuplets');
  197.  
  198.          Assigns the value 'I want sex' to MyString.
  199.  
  200.  
  201.  
  202. Strip
  203.  
  204.          Strip(L,C:char; Str:string): string;
  205.  
  206.          Returns the string with a specific character removed. The function is
  207.          passed three parameters; a character to indicate which part of the
  208.          string to strip, the character to strip, and the source string. The
  209.          strip can be performed on the left of the string, the right of the
  210.          string, the left and right, or all occurrences throughout the string.
  211.          The first parameter would be the character 'L', 'R', 'B' or 'A',
  212.          respectively. For example:
  213.  
  214.                   NoTees := Strip('A','T','THE TTT MAN');
  215.  
  216.          Assigns the value 'HE  MAN' to NoTees.
  217.  
  218.  
  219.  
  220. OverType
  221.  
  222.          OverType(N:byte; StrS,StrT:string):string;
  223.  
  224.          Places one string "on top of" another string and overlays the underly-
  225.          ing characters. The function is passed three parameters; the character
  226.          position in the target string to start the overtyping, the source
  227.          string, and the target string. For example:
  228.  
  229.                   Result := Overtype(8,'TechnoJock','I love you');
  230.  
  231.          Assigns the value 'I love TechnoJock' to Result.
  232.  
  233.  
  234.  
  235. PicFormat
  236.  
  237.          PicFormat(Input,Picture:string;Pad:char):string;
  238.  
  239.          This function was specifically used to complement the PictureIOOBJ
  240.          object used for obtaining formatted user input. The function returns a
  241.          formatted string, and is passed three parameters; the source string,
  242.          the picture string containing the format characters '!@*#', and the
  243.  
  244.  
  245. String Handling                                                             14-5
  246.  
  247. --------------------------------------------------------------------------------
  248.  
  249.          character used to pad the string if the source is not long enough.
  250.          Refer to chapter 11 for a full description of format characters. For
  251.          example:
  252.  
  253.                   TelStr := PicFormat('713493635','(###) ###-####','*');
  254.  
  255.          Assigns the value '(713) 493-635*' to TelStr.
  256.  
  257.  
  258.  
  259. TruncFormat
  260.  
  261.          TruncFormat(Input:string; Start,Len:byte; Pad:char):string;
  262.  
  263.          Truncates (or expands) a string from a specified character position.
  264.          The function is passed three parameters; the source string, the posi-
  265.          tion of the first character to extract, and the total length of the
  266.          returned string. For example:
  267.  
  268.                   NewStr := TruncFormat('Hidey Hidey Ho',9,10);
  269.  
  270.          Assigns the value 'dey Ho    ' to NewStr.
  271.  
  272.  
  273.  
  274. String Searching
  275.  
  276. FirstCapital
  277.  
  278.          FirstCapital(Str:string):char;
  279.  
  280.          Returns the first capital letter in a string. Note that this function
  281.          returns type char. If a capital letter is not found, a null (#0) is
  282.          returned. The function is passed one parameter; the source string. For
  283.          example:
  284.  
  285.                   MyChar := FirstCapital('7 File Save');
  286.  
  287.          Assigns the value 'F' to MyChar.
  288.  
  289.  
  290.  
  291. FirstCapitalPos
  292.  
  293.          FirstCapitalPos(Str:string):byte;
  294.  
  295.          Returns the character position of the first capital letter in a string.
  296.          Note that this function returns type byte. If a capital letter is not
  297.          found, a zero is returned. The function is passed one parameter; the
  298.          source string. For example:
  299.  
  300.                   ByteVar := FirstCapitalPos('how yer doin Bob');
  301.  
  302.          Assigns the value 14 to ByteVar.
  303.  
  304.  
  305.  
  306. 14-6                                                                User's Guide
  307.  
  308. --------------------------------------------------------------------------------
  309.  
  310. LastPos
  311.  
  312.          LastPos(C:char;Str:string):byte;
  313.  
  314.          Returns a byte indicating the position of the last occurrence of a
  315.          character in a string. If the character is not found, a zero is
  316.          returned. The function is passed two parameters; a character to search
  317.          for, and the source string. For example:
  318.  
  319.                   BytePos := LastPos('A','My Accommodation');
  320.  
  321.          Assigns the value 4 to BytePos.
  322.  
  323.  
  324.  
  325. LastPosBefore
  326.  
  327.          LastPosBefore(C:char;Str:string;Last:byte): byte;
  328.  
  329.          Returns a byte indicating the position of the last occurrence of a
  330.          character, up to a specified part of the string. If the character is
  331.          not found, a zero is returned. The function is passed three parameters;
  332.          a character to search for, the source string, and the position of the
  333.          last character to include in the search. For example:
  334.  
  335.                   SubPos := LastPosBefore('s','Mississippi',5);
  336.  
  337.          Assigns the value 4 to SubPos.
  338.  
  339.  
  340.  
  341. PosAfter
  342.  
  343.          PosAfter(C:char;Str:string;Start:byte):string;
  344.  
  345.          Returns a byte indicating the position of the first occurrence of a
  346.          character, starting from a specified position in the string. If the
  347.          character is not found, a zero is returned. The function is passed
  348.          three parameters; a character to search for, the source string, and the
  349.          position of the first character to include in the search. For example:
  350.  
  351.                   SubPos := PosAfter('s','Mississippi',5);
  352.  
  353.          Assigns the value 6 to SubPos.
  354.  
  355.  
  356. Word Management
  357.  
  358. WordCnt
  359.  
  360.          WordCnt(Str:string):byte;
  361.  
  362.          Returns the number of words in a string. The procedure is passed one
  363.          parameter; the source string. For example:
  364.  
  365.                   TotWords := WordCnt('eeny meeny miny mo');
  366.  
  367. String Handling                                                             14-7
  368.  
  369. --------------------------------------------------------------------------------
  370.  
  371.          Assigns the value 4 to TotWords.
  372.  
  373.  
  374.  
  375. PosWord
  376.  
  377.          PosWord(WordNo:byte; Str:string):byte;
  378.  
  379.          Returns the position of the first character of a specific word in a
  380.          string. The function is passed two parameters; the word number, and the
  381.          source string. If there are insufficient words in the string, a zero is
  382.          returned. For example:
  383.  
  384.                   MyWord := PosWord(4,'What a stupid idea son!');
  385.  
  386.          Assigns the value 15 to MyWord.
  387.  
  388.  
  389.  
  390. ExtractWords
  391.  
  392.          ExtractWords(StartWord,NoWords:byte; Str:string):string;
  393.  
  394.          Returns a substring containing a specified number of words (real words,
  395.          not computer words!) extracted from a source string. The function is
  396.          passed three parameters; the number of the first word to extract, the
  397.          total number of words to extract, and the source string. For example:
  398.  
  399.                   NewString := ExtractWords(5,3,'who the hell says
  400.                                                  censorship is good');
  401.  
  402.          Assigns the value 'censorship is good' to NewString.
  403.  
  404.  
  405.  
  406. Number Conversions
  407.  
  408. ValidInt
  409.  
  410.          ValidInt(Str:string):boolean;
  411.  
  412.          This function returns true if the source string represents a valid
  413.          integer, i.e. contains sensible numbers with no spaces or alpha charac-
  414.          ters. The only passed parameter is the source string. For example:
  415.  
  416.                   OK := ValidInt('23xy45');
  417.  
  418.          Assigns the value false to OK.
  419.  
  420.  
  421.  
  422. ValidHEXInt
  423.  
  424.          ValidInt(Str:string):boolean;
  425.  
  426.  
  427.  
  428. 14-8                                                                User's Guide
  429.  
  430. --------------------------------------------------------------------------------
  431.  
  432.          This function returns true if the source string represents a valid HEX
  433.          notation integer, i.e. contains the numbers 0 through 9, or letters 'A"
  434.          through 'F' with no spaces. The only passed parameter is the source
  435.          string. For example:
  436.  
  437.                   OK := ValidHEXInt('2E4A');
  438.  
  439.          Assigns the value true to OK.
  440.  
  441.  
  442.  
  443. ValidReal
  444.  
  445.          ValidReal(Str:string):boolean;
  446.  
  447.          This function returns true if the source string represents a valid real
  448.          number, i.e. contains numbers with no spaces or alpha characters. The
  449.          only passed parameter is the source string. For example:
  450.  
  451.                   OK := ValidReal('89.95');
  452.  
  453.          Assigns the value true to OK.
  454.  
  455.  
  456.  
  457. StrtoInt
  458.  
  459.          StrToInt(Str:string):integer;
  460.  
  461.          Converts a number string and returns an integer. If the string is not a
  462.          valid integer, a zero is returned. The function ValidInt can be used to
  463.          check that the string is convertible. For example:
  464.  
  465.                   Salary := StrToInt('30000');
  466.  
  467.          Assigns the value 30000 to Salary.
  468.  
  469.  
  470.  
  471. StrtoLong
  472.  
  473.          StrToLong(Str:string):longint;
  474.  
  475.          Converts a number string and returns a longint. If the string is not a
  476.          valid integer, a zero is returned. For example:
  477.  
  478.                   GoodSalary := StrToLong('300000');
  479.  
  480.          Assigns the value 300000 to GoodSalary.
  481.  
  482.  
  483.  
  484. StrToReal
  485.  
  486.          StrToReal(Str:string):extended;
  487.  
  488.  
  489. String Handling                                                             14-9
  490.  
  491. --------------------------------------------------------------------------------
  492.  
  493.          Converts a number string and returns a real. If the string is not a
  494.          valid real, a zero is returned. The function ValidReal can be used to
  495.          determine whether the string is convertible. For example:
  496.  
  497.                   Taxes := StrToReal('15643.27');
  498.  
  499.          Assigns the value 15643.27 to Taxes.
  500.  
  501.  
  502.  
  503. HEXStrToLong
  504.  
  505.          HEXStrToLong(Str:string):longint;
  506.  
  507.          Converts a hexadecimal string and returns a longint. If the string is
  508.          not a valid hex number, a zero is returned. The function ValidHEXInt
  509.          can be used to determine whether the string is convertible. For exam-
  510.          ple:
  511.  
  512.                   Val := HEXStrToLong('FF');
  513.  
  514.          Assigns the value 255 to Val.
  515.  
  516.  
  517.  
  518. IntToStr
  519.  
  520.          IntToStr(Number:longint):string;
  521.  
  522.          Returns a number converted to a string. The function is passed one
  523.          parameter; the source number of type byte, word, shortint, integer or
  524.          longint. For example:
  525.  
  526.                   NumStr := IntToStr(365);
  527.  
  528.          Assigns the value '365' to NumStr.
  529.  
  530.  
  531.  
  532. RealToStr
  533.  
  534.          RealToStr(Number:extended; Decimals:byte):string;
  535.  
  536.          Returns a real number converted to a string. The function is passed two
  537.          parameters; the source real number, and the number of decimal places.
  538.          If the number of decimal places is passed as FLOATING (a constant), the
  539.          function will return only the significant digits. For example:
  540.  
  541.                   MeatPie := RealToStr(3.546000,FLOATING);
  542.  
  543.          Assigns the value '3.546' to MeatPie.
  544.  
  545.  
  546. IntToHEXStr
  547.  
  548.          IntToHEXStr(Number:longint):string;
  549.  
  550. 14-10                                                               User's Guide
  551.  
  552. --------------------------------------------------------------------------------
  553.  
  554.          Returns a hexadecimal string representing the value of a number. The
  555.          function is passed one parameter; the source number, and it may be of
  556.          type byte, word, shortint, integer or longint. For example:
  557.  
  558.                   SillyPower := IntToHexStr(255);
  559.  
  560.          Assigns the value 'FF' to SillyPower.
  561.  
  562.  
  563.  
  564. RealtoSciStr
  565.  
  566.          RealToSciStr(Number:extended; D:byte):string;
  567.  
  568.          Returns a real number converted to a scientific notation string. The
  569.          function is passed two parameters; the source real number, and the
  570.          number of decimal places. For example:
  571.  
  572.                   Velocity := RealToSciStr(45678.984564,8);
  573.  
  574.          Assigns the value '4.56789846E+04' to Velocity.
  575.  
  576.  
  577.  
  578. NthNumber
  579.  
  580.          NthNumber(Str:string; Nth:byte):char;
  581.  
  582.          This function is actually used internally by the Toolkit in the totDATE
  583.          unit, but can be used if you can think of a reason! This function
  584.          returns the character representing a number found in the string. The
  585.          function is passed two parameters; the source string, and a byte indi-
  586.          cating the position of the number to be returned. For example:
  587.  
  588.                   NumChar := NthNumber('02/20/48',5);
  589.  
  590.          Assigns the value '4' to NumChar.
  591.  
  592.  
  593.  
  594. Examples
  595.  
  596.          Listed below are the demo programs DEMST1.PAS and DEMST2.PAS, which
  597.          illustrate how to use the string functions. Following each listing is a
  598.          figure detailing the resultant output.
  599.  
  600.          program DemoStringOne;
  601.          {demST1 - string functions}
  602.  
  603.          Uses DOS, CRT,
  604.               totFAST, totSTR;
  605.  
  606.          Var
  607.             DemoStr : string;
  608.  
  609.  
  610.  
  611. String Handling                                                            14-11
  612.  
  613. --------------------------------------------------------------------------------
  614.  
  615.          begin
  616.             ClrScr;
  617.             with Screen do
  618.             begin
  619.                DemoStr := '  TechnoJock''s Object Toolkit string demo   ';
  620.                WriteAt(5,1,lightgray,'Source String: ');
  621.                WriteAt(30,1,lightcyan,'"'+DemoStr+'"');
  622.                WriteAt(1,4,lightgray,'SetUpper:');
  623.                WriteAt(30,4,yellow,'"'+SetUpper(DemoStr)+'"');
  624.                WriteAt(1,5,lightgray,'SetLower:');
  625.                WriteAt(30,5,yellow,'"'+SetLower(DemoStr)+'"');
  626.                WriteAt(1,6,lightgray,'SetProper:');
  627.                WriteAt(30,6,yellow,'"'+SetProper(DemoStr)+'"');
  628.                WriteAt(1,7,lightgray,'Total words:');
  629.                WriteAt(30,7,lightgreen,IntToStr(WordCnt(DemoStr)));
  630.                WriteAt(1,8,lightgray,'Posn. Word 3:');
  631.                WriteAt(30,8,lightgreen,IntToStr(PosWord(3,DemoStr)));
  632.                WriteAt(1,9,lightgray,'Words 2..5 are:');
  633.                WriteAt(30,9,yellow,'"'+ExtractWords(2,4,DemoStr)+'"');
  634.                WriteAt(1,10,lightgray,'Strip Leading spaces:');
  635.                WriteAt(30,10,yellow,'"'+Strip('L',' ',DemoStr)+'"');
  636.                WriteAt(1,11,lightgray,'Strip Trailing spaces:');
  637.                WriteAt(30,11,yellow,'"'+Strip('R',' ',DemoStr)+'"');
  638.                WriteAt(1,12,lightgray,'Strip Lng. & Tng. spaces:');
  639.                WriteAt(30,12,yellow,'"'+Strip('B',' ',DemoStr)+'"');
  640.                WriteAt(1,13,lightgray,'Strip All spaces:');
  641.                WriteAt(30,13,yellow,'"'+Strip('A',' ',DemoStr)+'"');
  642.             end;
  643.             GotoXY(1,23);
  644.          end.
  645.  
  646.  
  647. Figure 14.1                                                             [SCREEN]
  648. String
  649. Functions
  650.  
  651.  
  652.  
  653.          program DemoStringTwo;
  654.          {demST2 - more string functions}
  655.  
  656.          Uses DOS, CRT,
  657.               totSTR;
  658.  
  659.          Const
  660.              MyInt:integer = 4000;
  661.              MyReal:real = 89.99;
  662.              MyIntStr = '8000';
  663.              MyRealStr = '89.95';
  664.              MyHexStr = 'FFFF';
  665.  
  666.  
  667.  
  668. 14-12                                                               User's Guide
  669.  
  670. --------------------------------------------------------------------------------
  671.  
  672.          begin
  673.             ClrScr;
  674.             writeln('Test number - ',MyInt);
  675.             writeln;
  676.             writeln('IntToStr:    ',IntToStr(MyInt));
  677.             writeln('IntToHEXStr: ',IntToHEXStr(MyInt));
  678.             writeln;
  679.             writeln;
  680.             writeln('Test number - ',MyReal:5:2);
  681.             writeln;
  682.             writeln('RealToStr: ',RealToStr(MyReal,5));
  683.             writeln('RealToStr: ',RealToStr(MyReal,1));
  684.             writeln('RealToStr: ',RealToStr(MyReal,FLOATING));
  685.             writeln('RealToSciStr: ',RealToSciStr(MyReal,FLOATING));
  686.             writeln;
  687.             writeln;
  688.             writeln('Test Strings: ',MyIntStr,' ',MyRealStr,' ',MyHEXStr);
  689.             writeln;
  690.             writeln('ValidInt: ',ValidInt(MyIntStr));
  691.             writeln('ValidInt: ',ValidInt(MyHEXStr));
  692.             writeln('ValidInt: ',ValidInt(MyRealStr));
  693.             writeln('ValidReal: ',ValidReal(MyIntStr));
  694.             writeln('ValidReal: ',ValidReal(MyRealStr));
  695.             GotoXY(1,23);
  696.          end.
  697.  
  698.  
  699.  
  700.  
  701. Figure 14.2                                                             [SCREEN]
  702. More String
  703. Functions
  704.  
  705.  
  706.  
  707. FmtNumberOBJ
  708.  
  709.          The totSTR unit includes the object FmtNumberOBJ. This object includes
  710.          function methods which accept numbers, and return them as formatted
  711.          strings. The object is used to provide formatting capabilities to the
  712.          number input fields in the totIO units (see page 11-21), but it may
  713.          also be used independently to provide sophisticated number formatting
  714.          control.
  715.  
  716.          The object can format both integer and real numbers, and the format
  717.          options include the following:
  718.  
  719.          Prefix     The number can be preceded by a character or short string,
  720.                     e.g. '$'.
  721.  
  722.          Suffix     The number can be succeeded by a character or short string,
  723.                     e.g. 'FFr'
  724.  
  725.  
  726. String Handling                                                            14-13
  727.  
  728. --------------------------------------------------------------------------------
  729.  
  730.          Sign       The number can be signed with + and/or -, parentheses on
  731.                     negative numbers, or DB/CR to indicate debit and credit.
  732.  
  733.          Separators The number can have thousands separated by a character, e.g.
  734.                     ','. Even the decimal place character and the character used
  735.                     to pad the string can be specified.
  736.  
  737.          Justificat The number can be left, right or center justified in the
  738.          ion        allotted space.
  739.  
  740.  
  741.  
  742.          To format numbers, you must initialize an instance of type FormatNum-
  743.          berOBJ, specify the appropriate format options, and call a function
  744.          method to return the number formatted as a string. The following
  745.          methods are available:
  746.  
  747.  
  748.          Init;
  749.  
  750.          This method initializes the object and must be called before the other
  751.          methods.
  752.  
  753.  
  754.          SetPrefixSuffix(P,S:string);
  755.  
  756.          Specifies the strings that will be used to precede and succeed the
  757.          number. Pass a null string, i.e. '', to suppress either the prefix or
  758.          suffix.
  759.  
  760.  
  761.          SetSign(S:tSign);
  762.  
  763.          The totSTR unit includes the declaration of an enumerated type tSign
  764.          with the following members: PlusMinus, Minus, Brackets, DbCr
  765.  
  766.          PlusMinus  Always precedes the number with a '+' or '-' to indicate the
  767.                     sign of the number.
  768.  
  769.          Minus      Only displays a '-', i.e. if the number is positive, the '+'
  770.                     is not displayed.
  771.  
  772.          Brackets   Negative numbers are enclosed in parentheses, e.g. (25.67).
  773.  
  774.          DbCr       If the number is negative it is succeeded with the string
  775.                     'DB', otherwise it is succeeded with the string 'CR'.
  776.  
  777.          Use this method to specify how the sign of a number will be displayed.
  778.  
  779.  
  780.          SetSeparators(P,T,D:char);
  781.  
  782.          Three different separators may be required to format a number, and this
  783.          method identifies them. The first parameter is the character used to
  784.          pad the string to the specified width, and common values are ' ' or
  785.          '*'. The second parameter specifies the character used to separate each
  786.  
  787. 14-14                                                               User's Guide
  788.  
  789. --------------------------------------------------------------------------------
  790.  
  791.          significant thousand, and typically has a value of ','. Specify #0, if
  792.          you don't want a thousands separator. The last parameter specifies the
  793.          character indicating the decimal place, e.g. '.'.
  794.  
  795.  
  796.          SetJustification(J:tJust);
  797.  
  798.          When the number is formatted, it can be left, center, or right justi-
  799.          fied. Pass a value of JustLeft, JustCenter or JustRight to identify the
  800.          required justification.
  801.  
  802.  
  803.          FormattedLong(Val:longint;Width:byte):string;
  804.  
  805.          Having set all the formatting options as required, call this method to
  806.          return a formatted string. The method is passed two parameters; any
  807.          whole number (i.e. byte, word, shortint, integer, longint), and the
  808.          width of the string. If the formatted string is too long to fit in the
  809.          specified width, an unformatted string is returned.
  810.  
  811.  
  812.          FormattedReal(Val:extended;DP,Width:byte):string;
  813.  
  814.          This function method is similar to the method FormattedLong, except it
  815.          is used to format real numbers. The method is passed three parameters;
  816.          the real number, the number of decimal places, and the width of the
  817.          returned string. The TotSTR unit includes a global constant FLOATING.
  818.          When FLOATING is specified as the number of decimal places, the Toolkit
  819.          will show all decimal places up to the last non-zero decimal.
  820.  
  821.  
  822.          Done
  823.  
  824.          This method disposes of the memory used by the instance, and should be
  825.          called when the object is no longer required.
  826.  
  827.  
  828.  
  829.  
  830.            Note: the totIO2 unit includes a global instance FmtNumberTOT
  831.            which is used to specify the default format for number input
  832.            fields. Refer to page 11-22 for further details.
  833.  
  834.  
  835.  
  836.  
  837.          Listed below is the demo program DEMST3.PAS, which illustrates how to
  838.          use FmtNumberOBJ objects.
  839.  
  840.          program DemoStringThree;
  841.          {demST3 - number formatting}
  842.  
  843.          Uses DOS, CRT,
  844.               totFAST, totSTR;
  845.  
  846.  
  847.  
  848. String Handling                                                            14-15
  849.  
  850. --------------------------------------------------------------------------------
  851.  
  852.          Var
  853.             Fmt: FmtNumberOBJ;
  854.  
  855.          begin
  856.             ClrScr;
  857.             with Fmt do
  858.             begin
  859.                Init;
  860.                writeln(FormattedLong(2000,15));
  861.                writeln(FormattedReal(2000,2,15));
  862.                SetSign(PlusMinus);
  863.                writeln(FormattedLong(2000,15));
  864.                SetSign(DBCR);
  865.                writeln(FormattedReal(2000,3,15));
  866.                SetPrefixSuffix('$','');
  867.                writeln(FormattedReal(2000,3,15));
  868.                SetSeparators('*',',','.');
  869.                writeln(FormattedReal(2000,3,15));
  870.                SetJustification(JustRight);
  871.                writeln(FormattedReal(2000,3,15));
  872.                Done;
  873.             end;
  874.             GotoXY(1,23);
  875.          end.
  876.  
  877.  
  878. Figure 14.3                                                             [SCREEN]
  879. Formatting
  880. Numbers
  881.