Re: BIG number as string
Re: BIG number as string
- Subject: Re: BIG number as string
- From: Paul Berkowitz <email@hidden>
- Date: Sat, 21 Dec 2002 16:58:20 -0800
On 12/21/02 4:18 PM, "Mr Tea" <email@hidden> wrote:
>
This from Paul Berkowitz - dated 21-12-02 11.10 pm:
>
>
> PythonMultiply(12345678, 87654321)
>
> -->"1082152022374638"
>
>
This still produces a real if either of the numbers is large enough to
>
compile as a real.
>
>
So, this line:
>
>
PythonMultiply(1082152022374638, 87654321)
>
>
...compiles to this:
>
>
PythonMultiply(1.08215202237464E+15, 87654321)
>
>
...and gives this result:
>
>
"9.48553007401e+22"
I'm sure this doesn't work yet for negative E numbers, but it does for your
example. I'll figure something out for negatives. Noyr that the number you
started with is so enormous that when it compiles to a real, it's actually
rounded, hence all the zeroes at the end of the result.
on PythonMultiply(number1, number2)
try
--works for string numbers too, not other strings
if not ({class of (number1 as number)} is in {integer, real} and
{class of (number2 as number)} is in {integer, real}) then error number -128
on error
display dialog "This routine works only for two numbers." buttons
{"OK"} default button 1 with icon 0
error number -128
end try
set newline to ASCII character of 10
set fn to (path to temporary items folder as string) & "xxxx.py"
set fnp to quoted form of POSIX path of fn
set f to open for access file fn with write permission
set eof f to 0
write ("print " & number1 & " * " & number2 & newline) to f
close access f
set ans to do shell script "python " & fnp
--do shell script "rm " & fnp -- not really needed with the eof 0 line
return ans
end PythonMultiply
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}
on error
set AppleScript's text item delimiters to {","}
set {integerPart, decimalPart} to basicReal's {text item 1, text
item 2}
end try
set AppleScript's text item delimiters to tids
set n to count decimalPart
if powersOfTen 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 & "." &
text (powersOfTen + 2) thru -1 of bigNumber
end if
return bigNumber
end NumberToString
set n to PythonMultiply(1082152022374638, 87654321)
set n to NumberToString(n)
--> "94855300740100000000000"
--
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.