scripting@wanadoo.nl




Fmt()

http://www.scriptlogic.com/kixtart/FunctionLibrary_FunctionList.aspx


actual FunctionLibrary list on 'Scriptlogic' site printer-friendly mirror of UDF topic on 'scripting@wanadoo.nl' site close
25 February 2002 Fmt()
AuthorNew Mexico Mark (wmarkh@aol.com)
Action Given a string and formatting codes, returns a formatted string.
SyntaxFmt (String, MinMax [,Control] ,[Pad]) 
Parameters
  • String (Required / String) Any expression that evaluates to a string or a type that can be directly converted to a string.
  • MinMax (Required / String) A *string* in the format "MIN[.MAX" where MIN is a number giving the minimum length of the returned string, and .MAX is the maximum length of the returned string. Ex. "20.20" or "80" or ".30".
  • Control (Optional / String) An integer sum of the following options (1-128 are internal): 1 - Left justify (right is default), 2 - Center justify (right is default - overrides 1), 4 - Pad with combination Tabs/spaces (spaces only is default), 8 - Reserved, 16 - Reserved, 32 - Reserved.
  • Pad (Optional / String) Padding character(s) to use. Default is space. 
  • RemarksThis is a huge help when formatted output is desired... i.e. columns of numbers or text like this. I kept the function name as short as possible, as it is my experience that it may be used repeatedly. This helps keep program lines managable.
    This function has been rewritten with more flexibility and with better control of UDF dependencies.* If you are using the old function, check your syntax before substituting functions.

    Essentially, what I did was to keep the core formatting internal to the function and use calls to other UDF's for the fancier stuff that may or may not be needed. This keeps the function reasonably small. 
    ReturnsThe formatted string. 
    DependenciesNone, UNLESS currency, number separator, or currency manipulation is required.

    Contitional dependencies on Flip_Numsep(), Flip_Currency() and Flip_Dec()
    Examples
    ; The following is a call to a file containing the following UDF's:
    ;  Flip_Dec()
    ;  Flip_Currency()
    ;  Flip_NumSep()
    ;  CALL 'd:\util\kss\UDF_Flip_Fun.kix'
    
    ; The following code is pretty complex because  I'm not only formatting the
    ; string, but I'm supplying quotes for clarity, then formatting THAT output
    ; for clarity. However, it demonstrates the flexibility of Fmt. I encourage
    ; anyone trying out this UDF to play with the "formatting fun" example code
    ; to see how easy it is to build beautifully formatted output with Fmt.
    
    $Q=Chr(34)
    $Str = "Hello World"
    "String supplied to examples 1-5 is: " + $Str ?
    "Example 01: " + Fmt($Q+Fmt($str,"")+$Q,30,1,".")       + " No formatting specified." ?
    "Example 02: " + Fmt($Q+Fmt($str,"20",0)+$Q,30,1,".")   + " Min=20, No Max, No Options." ?
    "Example 03: " + Fmt($Q+Fmt($str,"20",1)+$Q,30,1,".")   + " Min=20, No Max, Left Justify." ?
    "Example 04: " + Fmt($Q+Fmt($str,"22",2)+$Q,30,1,".")   + " Min=22, No Max, Center." ?
    "Example 05: " + Fmt($Q+Fmt($str,"20.5",1)+$Q,30,1,".") + " Min=20, Max=5, Left Justify." ?
    "Number supplied to examples 6-14 is 1024. Min=8, No Max." ?
    "Example 06: " + Fmt($Q+Fmt(1024,"8",256)+$Q,30,1,".")  + " Separation characters." ? ;*
    "Example 07: " + Fmt($Q+Fmt(1024,"8",512)+$Q,30,1,".")  + " Decimal." ? ;*
    "Example 08: " + Fmt($Q+Fmt(1024,"8",1024)+$Q,30,1,".") + " Currency."? ;*
    "Example 09: " + Fmt($Q+Fmt(1024,"8",768)+$Q,30,1,".")  + " Separation and decimal." ? ;*
    "Example 10: " + Fmt($Q+Fmt(1024,"8",1536)+$Q,30,1,".") + " Decimal and currency." ? ;*
    "Example 11: " + Fmt($Q+Fmt(1024,"8",1892)+$Q,30,1,".") + " Separation, decimal and currency." ? ;*
    "Example 12: " + Fmt($Q+Fmt(1024,"8",1280)+$Q,30,1,".") + " Separation and currency." ? ;*
    "Example 13: " + Fmt($Q+Fmt(1024,8,257)+$Q,30,1,".")    + " Separation and Left-Justify." ? ;*
    "Example 14: " + Fmt($Q+Fmt(1024,8,1)+$Q,30,1,".")      + " Left-Justify." ?
    
    $strTest = "1234567890" ?
    FOR $x = 1 TO 10
      "The time is @TIME: Your Enron stock value is:" + Fmt(SubStr($strTest,1,11 - $x),16,1892) ? ; *
      SLEEP 1
    NEXT
    
    ; * These require external UDF's. See documentation above.
    
    ; If you really want to have some fun, uncomment the following code.
    
    ;$str = " Formatting Fun! "
    ;FOR $x = 1 TO 80
    ;  Fmt($str,"" + $x + "." + $x) ?
    ;NEXT
    ;FOR $x = 80 TO 1 STEP -1
    ;  Fmt($str,"" + $x + "." + $x) ?
    ;NEXT
    ;FOR $x = 1 TO 80
    ;  Fmt($str,"" + $x + "." + $x,2,"*") ?
    ;NEXT
    ;FOR $x = 80 TO 1 STEP -1
    ;  Fmt($str,"" + $x + "." + $x,2,"*") ?
    ;NEXT
    ;FOR $x = 1 TO 80
    ;  Fmt(Fmt($str,"" + $x + "." + $x,2,"*"),80,2) ?
    ;NEXT
    ;FOR $x = 80 TO 1 STEP -1
    ;  Fmt(Fmt($str,"" + $x + "." + $x,2,"*"),80,2) ?
    ;NEXT
    ;FOR $x = 1 TO 80
    ;  Fmt(Fmt($str,"" + $x + "." + $x,2,"<*>"),"" + (80 - $x) + ".80",2,"<*>") ?
    ;NEXT
    ;FOR $x = 80 TO 1 STEP -1
    ;  Fmt(Fmt($str,"" + $x + "." + $x,2,"<*>"),"" + (80 - $x) + ".80",2,"<*>") ?
    ;NEXT
    ;End fun code.
    
    ; Console output from everything except fun code:
    
    ;String supplied to examples 1-5 is: Hello World
    ;Example 01: "Hello World"................. No formatting specified.
    ;Example 02: "         Hello World"........ Min=20, No Max, No Options.
    ;Example 03: "Hello World         "........ Min=20, No Max, Left Justify.
    ;Example 04: "     Hello World      "...... Min=22, No Max, Center.
    ;Example 05: "Hello"....................... Min=20, Max=5, Left Justify.
    ;Number supplied to examples 6-14 is 1024. Min=8, No Max.
    ;Example 06: "   1,024".................... Separation characters.
    ;Example 07: "   10.24".................... Decimal.
    ;Example 08: "   $1024".................... Currency.
    ;Example 09: "   10.24".................... Separation and decimal.
    ;Example 10: "  $10.24".................... Decimal and currency.
    ;Example 11: "  $10.24".................... Separation, decimal and currency.
    ;Example 12: "  $1,024".................... Separation and currency.
    ;Example 13: "1,024   ".................... Separation and Left-Justify.
    ;Example 14: "1024    ".................... Left-Justify.
    
    ;The time is 09:30:25: Your Enron stock value is:  $12,345,678.90
    ;The time is 09:30:26: Your Enron stock value is:   $1,234,567.89
    ;The time is 09:30:28: Your Enron stock value is:     $123,456.78
    ;The time is 09:30:29: Your Enron stock value is:      $12,345.67
    ;The time is 09:30:30: Your Enron stock value is:       $1,234.56
    ;The time is 09:30:31: Your Enron stock value is:         $123.45
    ;The time is 09:30:32: Your Enron stock value is:          $12.34
    ;The time is 09:30:33: Your Enron stock value is:           $1.23
    ;The time is 09:30:34: Your Enron stock value is:            $.12
    ;The time is 09:30:35: Your Enron stock value is:            $.01
    
     
    Source
    FUNCTION Fmt ($stext, $sminmax, OPTIONAL $ictrl, OPTIONAL $spad)
      ; $sText is an integer or string.
      ; $sMinMax must a string formatted as follows: "[MinLen][.MaxLen]"
      ; $sPad is a string of one or more characters to use as padding.
      ; $iCtrl is one or more of the following options (additive).
      ;
      ;    1 = Left-justify,
      ;    2 = Center,
      ;    4 = TABs and spaces.
      ;  256 = Add or remove separation characters,
      ;  512 = Add decimal in 100's place or remove any decimal chararacter,
      ; 1024 = Add or remove currency symbol
      ;
      ; NOTE: The first 8 bits (1 - 128) are built in to FMT(). Bits 9+ (256+ decimal) require external UDF's
      ;
      ; If the length of the integer or string is less than MinLen, it will be
      ; right-justified and padded with spaces,  unless specified otherwise by
      ; intControl.
      ; If MaxLen is supplied,  the string  (plus any padding added)  will  be
      ; truncated at that length.
      ;
      ; Returns:      Formatted string
      ; Dependencies: Contitional dependencies on Flip_NumSep(), Flip_Currency() and Flip_Dec()
      ;
      IF VarType($stext) < 2 OR VarType($stext) > 8
        RETURN
      ENDIF
    
      DIM $spadding, $btabs, $slpad, $srpad
      DIM $imin, $imax, $ipadlen, $ifmtlen, $itabwid, $icharwid, $i, $j, $k
    
      $itabspc=0
      $ifmtlen=0
      $ipadlen =0
      $itabwid=8
      $icharwid=1
    
      $stext    = '' + $stext
      $sminmax  = '' + $sminmax
      $ictrl    = 0 + $ictrl
      $spad     = '' + $spad
      IF ($spad = '')
        $spad = Chr(32)
      ENDIF
      IF $ictrl & 4
        $spad = Chr(9)
      ENDIF
      ; Horizontal Tab
      ;Calculate the width of the pad string
      FOR $i = 1 TO Len($spad)
        IF Asc(Substr($spad,$i,1)) = 9
          $ipadlen = $ipadlen + ($itabwid * (1 + ($ictrl & 2)/2))
        ELSE
          $ipadlen = $ipadlen + ($icharwid * (1 + ($ictrl & 2)/2))
        ENDIF
      NEXT
      ; Parse MinMax
      IF InStr($sminmax,".") > 0
        $imin = Val(Substr($sminmax,1,InStr($sminmax,".") - 1))
        $imax = Val(Substr($sminmax,InStr($sminmax,".") + 1))
      ELSE
        $imin = Val($sminmax)
        IF $imin = 0
          $imin = Len($stext)
        ENDIF
        $imax = 32000
      ENDIF
      ; UDF dependencies.
      IF $ictrl & 0256
        $stext = flip_numsep($stext)
      ENDIF
      IF $ictrl & 0512
        $stext = flip_dec($stext)
      ENDIF
      IF $ictrl & 1024
        $stext = flip_currency($stext)
      ENDIF
      ; After pre-processing, truncate string to $iMax length
      IF $imax < Len($stext)
        $stext = Substr($stext,1,$imax)
      ENDIF
      ; Calculate the width of the supplied string including TABS
      FOR $i = 1 TO Len($stext)
        IF Asc(Substr($stext,$i,1)) = 9
          $ifmtlen = $ifmtlen + $itabwid
        ELSE
          $ifmtlen = $ifmtlen + 1
        ENDIF
      NEXT
      IF ($ifmtlen < $imin)
        $j = $imin - ($ifmtlen + $ipadlen)
        $k = 0
        FOR $i = 0 TO $j STEP $ipadlen ; Number of sPad strings needed
          $spadding = $spadding + $spad
          $k = $k + $ipadlen
        NEXT
        $ifmtlen = $ifmtlen + $k
        $slpad=$spadding $srpad=$spadding
        FOR $i = 1 TO ($imin - $ifmtlen) ; Fill remainder with even number of spaces
          IF $ictrl & 2
            IF $i & 1
              $srpad = $srpad + ' '
            ELSE
              $slpad = ' ' + $slpad
            ENDIF
          ELSE
            $spadding = $spadding + ' '
          ENDIF
        NEXT
      ENDIF
      IF $ictrl & 1 ; LJust
        $fmt = $stext + $spadding
      ELSE
        ; RJust
        $fmt = $spadding + $stext
      ENDIF
      IF $ictrl & 2 ; Center
        $fmt = $slpad + $stext + $srpad
      ENDIF
      ;Recalculate the total length of the string.
      $ifmtlen = 0
      FOR $i = 1 TO Len($fmt)
        IF Asc(Substr($fmt,$i,1)) = 9
          $ifmtlen = $ifmtlen + $itabwid
        ELSE
          $ifmtlen = $ifmtlen + 1
        ENDIF
      NEXT
      FOR $i = 1 TO ($imin - $ifmtlen) ; Fill remainder with spaces
        $fmt = $fmt + ' '
      NEXT
      $fmt = Substr($fmt,1,$imax)
    ENDFUNCTION ; - Fmt -
     
      original source of UDF topic. show actual FunctionLibrary list on Scriptlogic site close top
              printer-friendly mirror of UDF topic on scripting@wanadoo.nl site  




    Copyright © 2003 www.scriptlogic.com & scripting@wanadoo.nl - last updated on 20 May 2003


    Site Meter