On Oct 25, 2008, at 6:03 PM, Luther Fuller wrote:
It must be getting too late. I escaped the repeat on mod not div.
Here is the correct code.
on format(nr, formatChar)
set chunkList to {}
repeat
set chunk to text -1 thru -3 of ("000" & ((nr mod 1000) as integer))
set chunkList to {chunk} & chunkList
set nr to nr div 1000
if nr = 0 then exit repeat
end repeat
set AppleScript's text item delimiters to {formatChar}
return chunkList as text
end format
I observed a slight anomaly with an input set to 9876543210.
Here's a variant which addresses that problem.
on FormatIntegerWithSeparator(theInteger, theSeparatorString)
set nlist to {}
repeat
set part to text -1 thru -3 of ("000" & ((theInteger mod 1000) as integer))
set nlist to {part} & nlist
set theInteger to theInteger div 1000
if theInteger = 0 then exit repeat
end repeat
set res to nlist as text
set AppleScript's text item delimiters to {theSeparatorString}
do shell script "tclsh <<< 'puts [string trimleft " & res & " 0]'"
end FormatIntegerWithSeparator
FormatIntegerWithSeparator(9.87654321E+9, ",")