• 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: Luther Fuller <email@hidden>
  • Date: Sat, 25 Oct 2008 16:33:34 -0500

On Oct 25, 2008, at 1:44 PM, Emile SCHWARZ wrote:

The question is simple:

I want to display a number. The number is the size of file(s) and it appears as 60123456 for example.

But I prefer to display it as 60 123 456 (depending on the numbers format set by country; the OS setting) or, 60,123,456 for US people.

In some other languages, there is a Format(integer,"#") command, but I do not found anything.

You can write a handler to format an integer. For example ...

on format(theNr)
	set digits to reverse of characters of (theNr as text)
	set formattedNr to ""
	repeat with i from 1 to (length of digits)
		set formattedNr to (item i of digits) & formattedNr
		if i mod 3 = 0 then set formattedNr to space & formattedNr
	end repeat
	return formattedNr
end format ------------------------------

my format(123456789) -- > " 123 456 789"

But, there a problem! Sooner or later, you will want to format something bigger, such as ...

set largeNr to (1234 * 1000000) + (567 * 1000) + 890
my format(largeNr) --> "1 .23 456 789 E+9"

Which isn't the result you want. AppleScript uses an exponential format to represent large integers and this complicates your problem. You will have to decompose the input number to un-exponential-format it.

Solution 1 = a handler

I once mentioned to someone that I could get AppleScript to print the value of n! as digits (avoiding exponential formating). And I could, it just took many minutes for the script to run. He suggested using Python and wrote a command file that would return the result as a numeric string ... and it ran in only a second. This is an incentive for me to learn Python, but I haven't done so, yet.

If you need to format large numbers, you can write a Python command file, put it in the Resources folder in your application bundle and call it from your script.

Solution 2 = a Python command

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

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