• 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: Philip Aker <email@hidden>
  • Date: Sat, 25 Oct 2008 18:55:28 -0700

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.
Gotta feelin' there may be other quirks though

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, ",")


Philip Aker
echo email@hidden@nl | tr a-z@. p-za-o.@

Democracy: Two wolves and a sheep voting on lunch.

 _______________________________________________
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: Luther Fuller <email@hidden>
References: 
 >How can I format an integer ? (From: Emile SCHWARZ <email@hidden>)
 >Re: How can I format an integer ? (From: Luther Fuller <email@hidden>)

  • Prev by Date: Re: How can I format an integer ?
  • Next by Date: Re: How can I format an integer ?
  • Previous by thread: Re: How can I format an integer ?
  • Next by thread: Re: How can I format an integer ?
  • Index(es):
    • Date
    • Thread