Number to String [WAS: Re: Date formatting]
Number to String [WAS: Re: Date formatting]
- Subject: Number to String [WAS: Re: Date formatting]
- From: Paul Berkowitz <email@hidden>
- Date: Tue, 04 Mar 2003 09:12:27 -0800
On 3/4/03 8:22 AM, "Paul Berkowitz" <email@hidden> wrote:
>
set bigNumber to text 1 thru (powersOfTen + 1) of bigNumber & "." &
>
text (powersOfTen + 2) thru -1 of bigNumber
I just noticed a flaw in my NumberToString() handler, which won't affect
anything to do with Unix dates, but would if used elsewhere. I check for the
decimal sign of E+ numbers: in many European countries it's "," not ".". If
the actual large number is not an integer but still has a decimal point when
written out "normally" then my handler is putting in a "." even when it
should be a ",". Not good. (As I say, this doesn't apply to seconds of a
date, but might in some scientific context. currencies, etc.) So here it is
fixed:
on NumberToString(bigNumber)
set bigNumber to bigNumber as string
if bigNumber does not contain "E+" then return bigNumber
set {tids, AppleScript's text item delimiters} to {AppleScript's text
item delimiters, {"E+"}}
try
set {basicReal, powersOfTen} to {bigNumber's text item 1,
(bigNumber's text item 2) as integer}
on error -- e.g. Python strings have lower-case "e", tids case-sensitive
set AppleScript's text item delimiters to {"e+"}
set {basicReal, powersOfTen} to {bigNumber's text item 1,
(bigNumber's text item 2) as integer}
end try
set AppleScript's text item delimiters to {"."}
try
set {integerPart, decimalPart} to basicReal's {text item 1, text
item 2}
set decimalSign to "."
on error
set AppleScript's text item delimiters to {","}
set {integerPart, decimalPart} to basicReal's {text item 1, text
item 2}
set decimalSign to ","
end try
set AppleScript's text item delimiters to tids
set n to count decimalPart
if powersOfTen is greater than or equal to n then
repeat (powersOfTen - n) times
set decimalPart to decimalPart & "0"
end repeat
set bigNumber to integerPart & decimalPart
else
set bigNumber to integerPart & decimalPart
set bigNumber to text 1 thru (powersOfTen + 1) of bigNumber &
decimalSign & text (powersOfTen + 2) thru -1 of bigNumber
end if
return bigNumber
end NumberToString
--
Paul Berkowitz
_______________________________________________
applescript-users mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/applescript-users
Do not post admin requests to the list. They will be ignored.