Money Script

Nicolas Cueto niconiko at gmail.com
Mon Aug 8 20:35:09 EDT 2011


Charles,

I had no idea what "dollar sign" script of mine you were referring to.
So, I looked in the mailing-list archives. Wow! I'd completely
forgotten about that.

> Your script is very cool

Thanks but it's embarrassingly inelegant.

Found something much cleaner, by Mark Wieder. It relies on the
"format" function.  Plus added my own function to first convert a cent
figure into a decimal figure. All of which I've included below.

Here's some conversion examples (original figure, as cents --> my
conversion --> Mark's conversion):

"1825" -->  "18.25"  -->  "$18.25"
"8" --> ".08" --> "$0.08"
"80" --> ".8"  --> "0.80"
"123456789" --> "123456789" --> "$1,234,567.89"

I think that takes care of what you're after. If not, just ask -- even
here onlist, cause that way you're sure to get advice much better than
mine.

Cheers.

--
Nicolas Cueto


////////////    S T A R T    O F     M O U S E     S C R I P T     //////



on mouseUp
  put field "Original figure in cents" into tCents
  // Call a function within a function!
  put fNumericToMoney(fNumericToCent(tCents)) into tDollarsAndCents
  put tDollarsAndCents into field "Dollar and cents figure"
end mouseUp



// My function. Which brute-force inserts a decimal.
// Probably there's a better way...

function fNumericToCent pNumeric
   switch (the number of chars in pNumeric)
      case 1
         put ".0" & pNumeric into tCentFigure
         break
      case 2
         put "." & pNumeric into tCentFigure
      default
         put pNumeric into tCentFigure
         put "." before char -2 of tCentFigure
   end switch
   return tCentFigure
end fNumericToCent




// The next function is by Mark Wieder.

function fNumericToMoney pNumeric
   local tMoney
   local x

   -- leading dollar sign and two digits for cents
   put format("$%.2f", pNumeric) into tMoney
   -- add the commas as necessary
   -- 4 is to ensure we don't get a comma before the dollar sign
   -- 6 here is skipping over the first three digits, the period, and the cents
   repeat with x=length(tMoney)-6 to 4 step -3
      -- +1 to skip over the dollar sign
      put comma before char x+1 of tMoney
   end repeat
   return tMoney
end fNumericToMoney




More information about the use-livecode mailing list