• 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: Thu, 6 Nov 2008 01:39:13 -0800

On 2008-11-06, at 00:21:25, Andrea D'Amore wrote:

on FormatInt(theNum, theSeparator)
if theNum > 999 then return (FormatInt(theNum div 1000, theSeparator) & theSeparator & text
-3 thru -1 of ("00" & ((theNum mod 1000) as text)))
return theNum as text
end FormatInt
FormatInt(9.99999999E+8, ",")

The example above doesn't give expected result on my system, I get "999,999,9,0" .
From what I can see there's a problem coercing a real in exponential form to integer, last comma is due to my locale setting.

Yes, I noticed that only after posting. Funny with all the previous chatter, nobody actually tested it!
You've located the problem exactly.

The short answer is that AppleScript's integer value limit before it gets converted to a double format is 2^29 - 1 (see the AppleScript Language Guide for a reference point).
The long answer is that it could be extended to larger integer values if it was tested for size first (by a 'reduce-prepend' recursive call (not done)) or by simply getting the string from another of the built-in scripting languages.

-- valid for up to E+11
set tnum to (9.99999999999E+11) as text
set nstr to (do shell script "tclsh <<< 'puts [expr " & tnum & "];'")

-- with error check
on FormatInt(theNumber, theSeparator)
if (theNumber > 536870911) then
error "number too big"
end if
if (theNumber > 999) then
set suffix to (theSeparator & text -3 thru -1 of ("00" & ((theNumber mod 1000) as text)))
return (FormatInt(theNumber div 1000, theSeparator) & suffix)
end if
return theNumber as text
end FormatInt
FormatInt(9.999999999E+11, ",")


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

References: 
 >Re: How can I format an integer ? (From: Barry Wainwright <email@hidden>)
 >Re: How can I format an integer ? (From: Philip Aker <email@hidden>)
 >Re: How can I format an integer ? (From: Andrea D'Amore <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