Re: Scientific notation
Re: Scientific notation
- Subject: Re: Scientific notation
- From: Jeffrey Mattox <email@hidden>
- Date: Thu, 1 May 2003 22:39:15 -0500
Paul:
You do good work, but all that AS for such a simple task makes me
cringe. How about this instead:
set theNumber to 3.0E-4
do shell script "perl -e 'print " & theNumber & "'"
--> "0.0003"
Jeff
At 7:09 PM -0700 5/1/03, Paul Berkowitz wrote:
On 5/1/03 5:24 PM, "Steve Roy" <email@hidden> wrote:
Hi,
In a script, I'm getting small real numbers such as 3.0E-4. When I turn them
to
string, I would like them to appear in normal notation "0.0003" instead of
"3.0E-4". Is that possible?
my NumberToString(3.0E-4)
--> "0.0003"
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 -- European
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
if powersOfTen > 0 then
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
else -- negative index
set bigNumber to integerPart & decimalPart
repeat (-powersOfTen - 1) times
set bigNumber to "0" & bigNumber
end repeat
set bigNumber to "0" & decimalSign & bigNumber
repeat while bigNumber ends with "0"
set bigNumber to text 1 thru -2 of bigNumber
end repeat
if bigNumber ends with "." then set bigNumber to bigNumber & "0"
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.
_______________________________________________
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.