Re: How can I format an integer ?
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 02:22:41 -0800
On 2008-11-06, at 02:05:31, Peter Waibel 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.
Unfortunately "number as text" return a string that is only useful
as display text.
"10.1 as text" returns "10,1" in some european countries.
As long as the result is used for display only thats fine.
But if the result must be used for further processing the implied
coersion is inconvenient.
I would like to have something like this:
1000.1 as localetext --> "1,000.1" -- english locale
1000.1 as localetext --> "1.000,1" -- german locale
1000.1 as localetext --> "1'000.1" -- swiss locale
1000.1 as localetext --> "1 000,1" -- french, canadian locale
1000.1 as text --> "1000.1" -- omitting locale settings
Meanwhile I use a handler GetRealAsStringWithDot() to solve this
problem.
Thanks Peter,
Just before your message I had received an off list post to the same
effect. I'm under the impression it would be difficult to file an
enhancement bug with a request to change the default "as text"
behavior (although I would prefer it myself). That's because there are
probably a zillion scripts already out there which rely on the current
behavior.
The fact that folks have to devise workarounds to get an integer
coerced to a human readable string such as have been proposed
certainly makes a bug fix by the AppleScript team seem warranted.
Peter
on GetRealAsStringWithDot(thisReal)
set errThrower to "GetRealAsStringWithDot() "
(*
returns: string, thisReal as string with a dot as decimal delimiter
thisReal: real
*)
try
try
if ("10.0" as real) = 10 then set systemDecimalDelimiterChar to "."
end try
try
if ("10,0" as real) = 10 then set systemDecimalDelimiterChar to ","
end try
set prevAppleScriptdelimiters to AppleScript's text item delimiters
set myString to thisReal as string
set AppleScript's text item delimiters to
{systemDecimalDelimiterChar}
set myList to every text item of myString
set AppleScript's text item delimiters to {"."}
set myDotString to myList as string
set AppleScript's text item delimiters to prevAppleScriptdelimiters
return myDotString
on error errMsg number errNum
try
set AppleScript's text item delimiters to prevAppleScriptdelimiters
end try
error errThrower & errMsg
end try
end GetRealAsStringWithDot
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