Re: How can I format an integer ?
Re: How can I format an integer ?
- Subject: Re: How can I format an integer ?
- From: Luther Fuller <email@hidden>
- Date: Sun, 26 Oct 2008 07:42:42 -0500
On Oct 25, 2008, at 8:55 PM, Philip Aker wrote:
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, ",")
Yes, there's a problem with leading zeros. Adding one line of code
fixes that.
I've also noticed that 'nr' can't have more than 15 digits.
on format(nr, formatChar)
-- nr can have no more than 15 digits
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 item 1 of chunkList to ((item 1 of chunkList) as integer as text)
-- added
set AppleScript's text item delimiters to {formatChar}
return chunkList as text
end format ------------------
_______________________________________________
Do not post admin requests to the list. They will be ignored.
AppleScript-Users mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
Archives: http://lists.apple.com/archives/applescript-users
This email sent to email@hidden