Re: BIG number as string
Re: BIG number as string
- Subject: Re: BIG number as string
- From: Kai Edwards <email@hidden>
- Date: Wed, 18 Dec 2002 15:10:35 +0000
Tue, 17 Dec 2002 17:05:21 -0500, Paul Skinner <email@hidden> wrote:
>
on NumberToText(theNumber)
>
set AppleScript's text item delimiters to ""
>
set the theNumberAsText to theNumber as text
>
if the theNumberAsText contains "E" then
>
set AppleScript's text item delimiters to "E"
>
set the theNumberAsText to every text item of theNumberAsText
>
set AppleScript's text item delimiters to ""
>
set theNumberString to item 1 of theNumberAsText
>
set theSign to character 1 of item 2 of theNumberAsText
>
set thePower to (characters 2 thru -1 of item 2 of theNumberAsText)
>
as text
>
set AppleScript's text item delimiters to "."
>
set theValue to every text item of theNumberString
>
set AppleScript's text item delimiters to ""
>
set theValue to theValue as text
>
if theSign is "+" then
>
set zeroes to ""
>
set valLength to (length of theValue) - 1
>
repeat (thePower - valLength) times
>
set zeroes to zeroes & "0"
>
end repeat
>
set theValue to theValue & zeroes
>
set theConvertedNumber to (characters 1 thru (thePower + 1) of
>
theValue) as text
>
try
>
set theConvertedNumber to theConvertedNumber & "." & (characters
>
(thePower + 2) thru length of theValue) as text
>
end try
>
else
>
set zeroes to ""
>
repeat thePower - 1 times
>
set zeroes to zeroes & "0"
>
end repeat
>
set theConvertedNumber to zeroes & theValue
>
end if
>
--repeat
>
else
>
return theNumber
>
end if
>
return theConvertedNumber
>
end NumberToText
That seems to do it nicely, Paul.
It occurred to me that it might be possible to abbreviate this approach a
little further - perhaps to something like this:
==============
on numToTxt(n)
set t to n as string
if "E" is not in t then return t
set {tid, text item delimiters} to {text item delimiters, "E"}
tell t's text items to set {i, d, s, x} to item 1's [NO BREAK]
{text 1 thru 1, text 3 thru -1} & item 2's {text 1 thru 1, text 2 thru -1}
set {text item delimiters, m, x, y, z} to [NO BREAK]
{tid, s = "-", x as number, d's length, ""}
if m then
set y to 1
else if y > x then
set d to d's text 1 thru x & "." & d's text (x + 1) thru -1
end if
repeat x - y times
set z to z & "0"
end repeat
if m then return z & i & d
i & d & z
end numToTxt
==============
This modification also appears to offer a speed benefit. OMM, all values are
returned in about the same time, except that the original version can take
around 15-20% longer to return some E+ real numbers - and up 100% longer for
E+ integers. (I suspect the main culprit is the try block.)
--
Kai
_______________________________________________
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.