Delimiting thousandths..converting 12345678 to 12, 345, 678 for example

Jim Ault JimAultWins at yahoo.com
Tue Jun 12 19:55:53 EDT 2007


On 6/12/07 3:52 PM, "Mark E. Powell" <runrev at aboutmyfiles.com> wrote:

> Thank you, Ken.  Works like a charm.
> -----------------------------------------------
> Here you go - this is based on a function that Mark Wieder created:
> 
> function formatThousands pNum,pSeparator
>   repeat with x = length(pNum)-3 to 3 step -3
>     put pSeparator before char x+1 of pNum
>   end repeat
>   return pNum
> end formatThousands
> 
> To call it:
> 
>   put formatThousands("10000000",",")
>   --> 10,000,000

I will offer a slight improvement to make it a bit more universal
-- neg numbers
--preceeded by $
--decimals

function formatThousands pNum,pSeparator
  repeat while char 1 of pNum is not a number then
    put char 1 of pNum after holdPrefix
    put char 2 to -1 of pNum into pNum
  end repeat
  set the itemDel to "."
  put item 2 of pNum into holdDec
  put item 1 of pNum into pNum
 >   repeat with x = length(pNum)-3 to 3 step -3
>     put pSeparator before char x+1 of pNum
>   end repeat
  return (holdPrefix & pNum & "." & holdDec)
end formatThousands

Jim Ault
Las Vegas





More information about the use-livecode mailing list