Re: How can I format an integer ?
Re: How can I format an integer ?
- Subject: Re: How can I format an integer ?
- From: André Berg <email@hidden>
- Date: Thu, 06 Nov 2008 17:28:20 +0100
Hi Philip,
Thanks (again :) ) for your input and help.
So there is no AppleScript native setting. Just as I thought...
For me it's like this:
Once you need to use 'do shell script' your not limited to what
you can do so, it IS a perfectly valid workaround, which I use
almost all the time, but even then it comes at a price. And in case
of the ever occasional performance critical code - a very high price.
On my MacPro 3000Mhz octo-core which I bought two months ago
'do shell script' takes approx 0.070 - 0.080 s (avg of 1000 execs)
not counting the time for the actual shell script to run.
So far in my own code I have come to find there are only two major
workaround paths:
One way is AppleScript based with switching the text item delimiters
a few times, based on 'user locale of (system info)' to account for
all possible locale separators one needs to target.
Or, the second way is to use 'do shell script' with Ruby, Python, Tcl,
bc, sed
...
the list goes on.
My personal favorite with this route is engineering wrapper handlers around
Python's decimal module. It's really really fast and naturally has arbitrary
precision support.
My second favorite way for handling numbers too large for AppleScript's
real ('doubl' type) to string coercions is using a shell script pipe along
the lines of "echo <bc script with values intertwined from AS> | sed -E
-e s/,/./ | bc -l"
which I only use for more than basic math calculations like sine, cosine,
etc. to arbitrary precision when I can't use Satimage's OSAX.
This btw on my system is much faster than using Python's numpy but ofc
doesn't have as much possibilities tied in.
I have also found that you can use normal Python syntax in HERE strings
once you figure out the backslash-salad that is.
The only moot point about 'do shell script' is that it uses a default
/bin/sh
which always uses a POSIX style locale with English style decimal and
monetary
separators while AppleScript itself uses the values found with 'locale
-ck LC_ALL'
from a Terminal. That's why I have to always pipe in the 'sed -E -e s/,/./'
command to handle this discrepancy.
Thing is I don't think Apple regards this as a bug as their TechNote [1] on
'do shell script' suggests the following:
----
Q: Why doesn’t do shell script work exactly like Terminal?
A: For two reasons: first, it helps guarantee that scripts will run on
different
systems without modification. If do shell script used your default shell
or PATH,
your script would probably break if you gave it to someone else.
Second, it matches shell escape mechanisms in other languages, such as Perl.
----
That A: sentence really ought to be
"first, it helps guarantee that scripts will run on different [English
locale based]
systems without modification."
Of course from a security standpoint and with respect to everything but
locale
settings they are right with this approach. However I don't see so much harm
by 'do shell script' reading the locale settings like the Terminal and
AppleScript
per se do. Also note, all this only applies when you mix AppleScript
syntax and
'do shell script' output on non English locale based systems.
My proposal:
A simple property of the top level script object (AppleScript) for
setting the
decimal and monetary separators would be very elegant in alleviating all of
these problems. Of course it would require one or two lines of code extra
but I can happily live with that. And this would also mean that none of the
old scripts break if we set the default value to the one that AppleScript
always had, so that Scripters who are writing scripts now can make use of it
if they want/need to.
Cheers
André
[1] http://developer.apple.com/technotes/tn2002/tn2065.html
--- Original Nachricht ---
Absender: Philip Aker
Datum: 06.11.2008 10:54 Uhr
On 2008-11-06, at 01:39:16, André Berg wrote:
I get the same results and its not just limited to reals in scientific
notation, but any real to string coercion.
Ugh, I don't know how many workarounds I had to script because
of that pesky locale settings which is reckognized by AppleScript
per se but not by 'do shell script' which uses a virgin/default
/bin/sh ><
I was always wondering if there wasn't some (hidden?) setting one
could change like with text item delimiters. You set it before you
do the conversion from real to string and AS uses that setting.
Or do I really have to pipe all my shell scripts thru 'sed -E -e
s/,/./g'
before actually using them?
Hi André,
My previous post arrived at an explanation and an alternate solution.
Apologies if this is regarded as trolling, but I thought a discussion
of AppleScript's default decimal separator still has something to do
with formatting a number.
Wail away, but better still file a bug. It's a serious limitation that
should be addressed. Maybe one big headache for the fixer is that it's
got 15+ years of baggage to account for one way or another.
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.
Philip Aker
_______________________________________________
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