Re: BIG number as string
Re: BIG number as string
- Subject: Re: BIG number as string
- From: Paul Skinner <email@hidden>
- Date: Wed, 18 Dec 2002 14:48:01 -0500
On Wednesday, December 18, 2002, at 09:48 AM, Arthur J. Knapp wrote:
Date: Tue, 17 Dec 2002 22:51:13 +0000
Subject: BIG number as string
From: julifos <email@hidden>
I'd like retrieve a string from a big-numbers-calculation, but I
can't :-(
55555 * 55555
--> 3086358025
--> rather than 3.086358025E+9
Nigel Garvey has an exceptionally robust and well tested handler for
this:
<http://files.macscripter.net/ScriptBuilders/ScriptTools/numToStr.sit>
snip
I can't say without a doubt that my reworked version is as robust as
NG's since has hasn't had a hand at hurting it yet. But I found no
disparity between it and NG's version over several thousand random
trial values.
on NumberToText(theNumber)
set negative to false
set AppleScript's text item delimiters to ""
set the numberText to theNumber as text
if the numberText contains "E" then
if character 1 of numberText is "-" then set {negative, numberText}
to {true, (text 2 thru end of numberText) as text}
set AppleScript's text item delimiters to "E"
set {num, exp} to text items of numberText
set AppleScript's text item delimiters to (0 as real as string)'s
item 2
set theNumberString to text items of num
if item 2 of theNumberString is "0" then set item 2 of
theNumberString to ""
set AppleScript's text item delimiters to ""
set theValue to theNumberString as text
set {theSign, thePower} to {character 1 of exp, ((characters 2 thru
-1 of exp)) as text}
if theSign is "+" then
set zeroLength to thePower - ((the length of theValue) - 1)
set zeroes to ""
repeat zeroLength times
set zeroes to zeroes & "0"
end repeat
set theValue to theValue & zeroes
else
set zeroLength to thePower - 1
set zeroes to ""
repeat zeroLength times
set zeroes to zeroes & "0"
end repeat
set theValue to "0." & zeroes & theValue
end if
if negative then set the theValue to "-" & theValue
return theValue
else
return theNumber as text
end if
end NumberToText
_______________________________________________
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.