Money

Nicolas Cueto nicolas_cueto at yahoo.com
Thu Apr 14 19:10:27 EDT 2005


Hello Hershel,

> How do I handle money amounts in a fld, e.g. "," or "." ?

I'm not sure what you mean by "handle" here.

If, for example, you mean *adding* a pair of dollar amounts (i.e. "$1,999.09
+ $0.91 = $2,000"), then below is a script that'll do that.

Basically, the script (1) strips the dollar symbol and commas from two
field-entered amounts, and then, after adding those two amounts, (2)
reinserts the dollar symbol and commas into the summed amount. (Note: you'll
of course need to modify the script somewhat if, say, you're working with
more than two fields, or the money amount is not dollars, or you want to do
other mathematical functions besides adding.)

Cheers,
Nicolas Cueto
(land of the yen)

-- START OF SCRIPT

on mouseUp
 -- GATHER THE MONEY INFO
 -- FROM, SAY, TWO FIELDS
  put field "f1" into tOne
  put filterThis(tOne) into tFilteredOne
  put field "f2" into tTwo
  put filterThis(tTwo) into tFilteredTwo
  put (tFilteredOne + tFilteredTwo) into tTotal
  put dollarize(tTotal) into field "fTotal"
end mouseUp

function filterThis tOriginal
  -- REMOVE ANY CHARACTER NOT A
  -- NUMBER OR DECIMAL POINT
  put empty into tFiltered
  repeat for each char tChar in tOriginal
    -- USE A "REGULAR EXPRESSION" AS A FILTER
    if matchText(tChar, "^[0-9.]+$") then
      put tChar after tFiltered
    else
    end if
  end repeat
  -- RETURN THE NOW-FILTERED FIGURE
  -- BACK TO THE "mouseUp" HANDLER
  return tFiltered
end filterThis

function dollarize tOriginal
  -- CONVERT, SAY, "1234.56"
  -- TO "$1,234.56"
  put the number of characters in tOriginal into tTotalChar
  put 0 into tCommaPlaceholder
  put empty into tDollarized
  repeat with i = tTotalChar down to 1
    put char i of tOriginal into tChar
    -- FIRST, CHECK FOR DECIMAL POINT
    if tChar is "." then
      put tChar before tDollarized
      put 0 into tCommaPlaceHolder
      next repeat
    end if
    -- SECOND, CHECK WHETHER tChar
    -- IS THE THIRD NUMBER IN THE SERIES
    if tCommaPlaceholder is 3 then
      put "," before tDollarized
      put 0 into tCommaPlaceholder
    end if
    put tChar before tDollarized
    add 1 to tCommaPlaceHolder
  end repeat
  put "$" before tDollarized
  -- RETURN THE NOW-DOLLARIZED FIGURE
  -- BACK TO THE "mouseUp" HANDLER
  return tDollarized
end dollarize

-- END OF SCRIPT



More information about the use-livecode mailing list