• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: How can I format an integer ?
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: How can I format an integer ?


  • Subject: Re: How can I format an integer ?
  • From: Barry Wainwright <email@hidden>
  • Date: Tue, 04 Nov 2008 21:27:07 +0000

here's the routine I use:

on commafy(theNum)
-- A recursive routine that converts an integer to a western formatted
-- number with commas between the 'thousand' groupings
if theNum > 999 then return (commafy(theNum div 1000) & "," & text -3 thru -1 of ("00" & ((theNum mod 1000) as text)))
return theNum as text
end commafy



-- Barry


On 26 Oct 2008, at 12:42, Luther Fuller wrote:

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 (applescript- email@hidden)
Help/Unsubscribe/Update your Subscription:
Archives: http://lists.apple.com/archives/applescript-users


This email sent to email@hidden

_______________________________________________ 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
  • Follow-Ups:
    • Re: How can I format an integer ?
      • From: Philip Aker <email@hidden>
  • Prev by Date: Re: Re. Print to PDF work flow
  • Next by Date: Stupid AppleScript tricks with Pages and Keynote
  • Previous by thread: Re: Re. Print to PDF work flow
  • Next by thread: Re: How can I format an integer ?
  • Index(es):
    • Date
    • Thread