scripting@wanadoo.nl




Flip_Dec()

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
12 February 2002 Flip_Dec()
AuthorW.M. Hinsch (New Mexico Mark)
Action Add or remove the decimal character (as defined on your system) in a number/string.
SyntaxFlip_Dec (String, PositiveInteger) 
Parameters
  • String (Required / String) A number or string. If it has a decimal character, that character will be removed. If not, it will be added.
  • PositiveInteger (Optional / Numeric) Integer of the position between 1 and 30,000 giving the decimal position. I.e. 2 = nnn.nn If not supplied, the default is 2. 
  • RemarksThis function adds or removes the decimal character from a number. Ideally, the number will be supplied as a string (this is the only option for supplying numbers with a decimal).

    This function may use Flip_Numsep() or Flip_Currency() if number separators and/or a currency symbol are present. 
    ReturnsA string. 
    DependenciesNone. 
    Examples
    $str = "98765.43210" ?
    FOR $i = 11 TO 0 STEP -1
      $t = SubStr($str,1,$i)
      "Flip_Dec(" + Chr(34) + $t + Chr(34) + ") = " + Chr(34) + Flip_Dec($t) + Chr(34) ?
    NEXT
    $t = "987654321" ?
    FOR $i = -1 TO 12
      "Flip_Dec(" + Chr(34) + $t + Chr(34) + "," + $i + ") = " + Chr(34) + Flip_Dec($t,$i) + Chr(34) ?
    NEXT
    $t = "6543.21" ?
    "Flip_Dec(" + Chr(34) + $t + Chr(34) + ",4) = " + Chr(34) + Flip_Dec($t,4) + Chr(34) ?
    
    ; Console Output (Example)
    
    ;Flip_Dec("98765.43210") = "9876543210"
    ;Flip_Dec("98765.4321") = "987654321"
    ;Flip_Dec("98765.432") = "98765432"
    ;Flip_Dec("98765.43") = "9876543"
    ;Flip_Dec("98765.4") = "987654"
    ;Flip_Dec("98765.") = "98765"
    ;Flip_Dec("98765") = "987.65"
    ;Flip_Dec("9876") = "98.76"
    ;Flip_Dec("987") = "9.87"
    ;Flip_Dec("98") = ".98"
    ;Flip_Dec("9") = ".09"
    ;Flip_Dec("") = ".00"
    
    ;Flip_Dec("987654321",-1) = "9876543.21"
    ;Flip_Dec("987654321",0) = "9876543.21"
    ;Flip_Dec("987654321",1) = "98765432.1"
    ;Flip_Dec("987654321",2) = "9876543.21"
    ;Flip_Dec("987654321",3) = "987654.321"
    ;Flip_Dec("987654321",4) = "98765.4321"
    ;Flip_Dec("987654321",5) = "9876.54321"
    ;Flip_Dec("987654321",6) = "987.654321"
    ;Flip_Dec("987654321",7) = "98.7654321"
    ;Flip_Dec("987654321",8) = "9.87654321"
    ;Flip_Dec("987654321",9) = ".987654321"
    ;Flip_Dec("987654321",10) = ".0987654321"
    ;Flip_Dec("987654321",11) = ".00987654321"
    ;Flip_Dec("987654321",12) = ".000987654321"
    
    ;Flip_Dec("6543.21",4) = "65.4321"
    
     
    Source
    FUNCTION Flip_Dec ($svar, OPTIONAL $ipos)
      ; Takes an expression that can be directly converted to a string and returns
      ; a string with a system decimal added (or removed if already present) to the
      ; hundreds place. This can provide a work-around for currency by using
      ; integers * 100 and displaying the results with this function.
      ;
      IF VarType($svar) < 2 OR VarType($svar) > 8
        RETURN
      ENDIF
    
      DIM $cdec, $csep, $ccur, $ilen, $sreg, $ibeg, $iend, $i, $j, $k, $c
      DIM $bcur, $bsep, $bpos
      $svar  = "" + $svar
      $ipos = 0 + $ipos
      $ilen  = Len($svar)
      $ibeg  = 1
      $iend  = $ilen
      IF ($ipos < 1)
        $ipos = 2
      ELSE
        $bpos=1
      ENDIF
      $sreg  = 'HKCU\Control Panel\International'
      $cdec = ReadValue($sreg,'sDecimal')
      $csep = ReadValue($sreg,'sThousand')
      $ccur = ReadValue($sreg,'sCurrency')
      IF InStr($svar,$cdec) ; One or more decimals present... remove.
        FOR $i = $ibeg TO $iend
          $c = Substr($svar,$i,1)
          IF ($c <> $cdec)
            $k = "" + $k + $c
          ENDIF
        NEXT
        IF $bpos
          $k = flip_dec($k,$ipos)
        ENDIF
        $flip_dec = $k
      ELSE ; Add decimal
        IF InStr($svar, $ccur)
          $svar=flip_currency($svar)
          $bcur=1
        ENDIF
        IF InStr($svar, $csep)
          $svar=flip_numsep($svar)
          $bsep=1
        ENDIF
        $ilen = Len($svar)
        $iend = $ilen
        IF ($iend < $ipos)
          $iend=$ipos
        ENDIF
        FOR $i = $ibeg TO $iend
          IF ($i <= $ilen)
            $k = "" + $k + Substr($svar,$i,1)
          ELSE
            $k = "0" + $k
          ENDIF
          ; "Beg=$iBeg End=$iEnd Len=$iLen iPos=$Ipos i=$i k=$k" ?
          IF ($ilen = $ipos+$i)
            $k = "" + $k + $cdec
          ENDIF
          IF ($ipos >= $ilen) AND ($ipos = $i)
            $k = "" + $cdec + $k
          ENDIF
        NEXT
        $flip_dec = $k
        IF $bsep
          $flip_dec=flip_numsep($flip_dec)
        ENDIF
        IF $bcur
          $flip_dec=flip_currency($flip_dec)
        ENDIF
      ENDIF
    ENDFUNCTION ; - Flip_Dec -
     
      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