formatting dollar signs, commas, and quotes

Developers
2007-10-23
2012-08-14
  • Nobody/Anonymous

    Does any body have a cool way to change 6948.32 to "6,948.32" I mean with the actual quotes as shown. Its to use in an xml conversion utility. Could be any number of characters in lenght.

    --Gary

     
    • Nobody/Anonymous

      sorry, I may not have made the problem difficult enough. The starting point is ' 6948.32' (leading blanks).

       
    • Nobody/Anonymous

      not sure if it's cool but this seems to work.

      InNum = ' 12345678901234567.89'
      say InNum
      InNum = Strip(InNum)
      p4 = ''
      parse var InNum p1 '.' p2
      p1 = Reverse(p1)
      do while p1 <> ''
      Parse var p1 p3 4 p1
      If p4 = '' then p4 = p3
      Else p4 = p4','p3
      end
      say '"'Reverse(p4)'.'p2'"'

      Jon
      jon@us.ibm.com

       
    • Nobody/Anonymous

      Jon--

      Way cool! Thanks.

      --Gary

       
      • Nobody/Anonymous

        Hi,

        A long time ago, I wrote an external function class (file name "XFUNCT.CLS") I which I put the following routine:

        / -------------------------------------------------------------------------- /
        / - Routine name : MASK - /
        / - Date re-written : 12/02/1999 (transfered from CMS) - /
        / - Descriptive name : takes a number and returns a string in comma- - /
        / - delimited format eg. mask(a,1234.456,2) returns - /
        / - 1,234.56 - /
        / - Requires : 3 arguments, a 4th is optional - /
        / - arg(1)= the format(A = trailing minus, B = ending) - /
        / - arg(2)= the actual number - /
        / - arg(3)= the decimal places - /
        / - arg(4)= the length of the return string (optional) - /
        / -------------------------------------------------------------------------- /
        ::ROUTINE Mask PUBLIC
        / count with 31 digits /
        Numeric Digits 31
        / if Option not type 'A' or 'B' /
        / error code Err(1) /
        If arg(1) \= 'A' & arg(1) \= 'B'
        Then Return 'Error in "mask" : arg(1) is not "A" or "B"'~left(80)
        / if Number not type numeric return /
        / error code Err(2) /
        If arg(2)~datatype \= 'NUM'
        Then Return 'Error in "mask" : arg(2=number) is not numerical'~left(80)
        / if DecimalPlaces not numeric /
        / return error code Err(3) /
        If arg(3)~datatype <> 'NUM'
        Then Return 'Error in "mask" : arg(3=decimal places) is not numerical'~left(80)
        / if StringLength not blank and not /
        / numeric return error code Err(4) /
        If arg(4,e) = 1 & arg(4)~datatype('NUM') \= 1
        Then Return 'Error in "mask" : arg(4=length) is not numerical'~left(80)
        / round off the Number to the /
        / DecimalPlaces and parse the result /
        / in Integer and Decimals /
        Parse value format(arg(2),,arg(3),0) with Integer "." Decimals
        / Make Integer positive /
        Integer = Integer~abs
        / reverse the digits in Integer so /
        / we can parse them in groups of 3 /
        IntegerReversed = Integer~reverse
        IntegerOut = ''
        / while three digits or more remain /
        / in IntegerReversed, take each group /
        / of three remaining digits, and then /
        / join them to the end of the /
        / IntegerOut variable and add a comma /
        Do While IntegerReversed~length > 3
        Parse var IntegerReversed Group 4 IntegerReversed
        IntegerOut = IntegerOut||Group||','
        end
        / concatenate the digits that remain /
        / after above loop /
        IntegerOut = IntegerOut||IntegerReversed
        / if Number was negative, restore the /
        / minus sign /
        If arg(2) < 0 then
        Do
        If arg(1) = 'A' then IntegerOut = IntegerOut||'-'
        If arg(1) = 'B' then IntegerOut = '-'||IntegerOut
        end
        / restore the proper order of the /
        / digits and add the decimal point /
        / and decimals /
        If arg(3) = 0 then
        IntegerOut = IntegerOut~reverse
        Else IntegerOut = IntegerOut~reverse||'.'||Decimals
        / return the string to the caller /
        If arg(4,e) = 0 then
        Return IntegerOut~right(15)
        Else Return IntegerOut~right(arg(4))
        / /

        The function is called with 3 mandatory arguments and 1 optional (please read through the "cls" file).

        An example to call the function is underneath:
        / TestFunction /

        amount = 1228568.5667

        do i=15 to 25 by 1 -- do loop is just to illustrate the 4th argument
        say mask('A',amount,2,i)
        end

        exit

        ::requires 'xfunct.cls'
        / /

        I hope this can be found usefull.

        D. De Wilde

         

Log in to post a comment.

Get latest updates about Open Source Projects, Conferences and News.

Sign up for the SourceForge newsletter:





No, thanks