Programing style

Ken Ray kray at sonsothunder.com
Thu Nov 16 20:30:05 CST 2006


On 11/16/06 3:01 PM, "Hershel Fisch" <hershf at rgllc.us> wrote:

> Hi all, I'd like to know how others write a function like this.
> TIA Hershel
> 
> function fNumericToMoney
>   put param(1) into tParam
>   if tParam contains "." then
>     put offSet(".",tParam)-1 into tTs
>     put 3 into tC
>   else
>     put the number of chars in tParam into tTs
>     put 0 into tC
>   end if
>   repeat while tTs >3
>     add 3 tC
>     put "," before char -tC in tParam
>     add 1 to tC
>     subtract 3 from tTs
>   end repeat
>   return "$" & tParam
> end fNumericToMoney

Here's mine:

function stsConvertToDollars pAmt
  split pAmt by "."  -- pAmt[1] is the dollars, pAmt[2] is the cents
  if pAmt[2] = "" then put "00" into pAmt[2]
  if length(pAmt[2]) = 1 then put "0" after pAmt[2]
  
  put trunc(length(pAmt[1])/3) into tNumLoops
  put "" into tDollars
  repeat tNumLoops
    put "," & char -3 to -1 of pAmt[1] before tDollars
    delete char -3 to -1 of pAmt[1]
  end repeat
  put pAmt[1] & tDollars & "." & pAmt[2] into tRetVal
  if char 1 of tRetVal = "," then delete char 1 of tRetVal
  return "$" & tRetVal
end stsConvertToDollars

Basically the same repeat loop but slightly different in parsing...

Ken Ray
Sons of Thunder Software, Inc.
Web site: http://www.sonsothunder.com/
Email: kray at sonsothunder.com





More information about the use-livecode mailing list