Re: How to convert number from exponential form?
Re: How to convert number from exponential form?
- Subject: Re: How to convert number from exponential form?
- From: KOENIG Yvan <email@hidden>
- Date: Mon, 12 Jan 2009 18:29:08 +0100
Le 12 janv. 2009 à 15:25, Barry Wainwright a écrit :
set theNumbers to {1.0139648E+5, 1.0139648E+7, 1.0139648E+9}
repeat with aNumber in theNumbers
log my normalise(aNumber)
end repeat
on normalise(theNum)
try
set {oldTIDS, AppleScript's text item delimiters} to
{AppleScript's text item delimiters, {"E"}}
set {theSignificand, theExponent} to text items of (theNum as text)
set {oldTIDS, AppleScript's text item delimiters} to
{AppleScript's text item delimiters, {"."}}
set {part1, part2} to text items of theSignificand
set AppleScript's text item delimiters to oldTIDS
on error errMsg number errNum
-- no 'E' in the number!
return theNum as text
end try
set theExponent to theExponent as integer
set part2Length to count part2
if theExponent > part2Length then
set theZeros to ""
repeat (theExponent - part2Length) times
set theZeros to theZeros & "0"
end repeat
return part1 & part2 & theZeros
else if theExponent = part2Length then
return part1 & part2
else
return part1 & (text 1 thru theExponent of part2) & "." & text
(theExponent + 1) thru -1 of part2
end if
end normalise
--
(1) I must apologize because my proposal works only if the exponent
is greater than eight.
(2) This one works flawlessly … on machines using the period as a
decimal separator.
Here, as I uses the comma as decimal separator the result is:
(*1,0139648E+5*)
(*1,0139648E+7*)
(*1,0139648E+9*)
because the searched period is unavailable.
Here is an enhanced version working worldwide:
set theNumbers to {1.0139648E+5, 1.0139648E+7, 1.0139648E+9}
repeat with aNumber in theNumbers
log my normalise(aNumber)
end repeat
on normalise(theNum)
try
set deci to character 2 of (0.5 as text)
set {oldTIDS, AppleScript's text item delimiters} to {AppleScript's
text item delimiters, {"E"}}
set {theSignificand, theExponent} to text items of (theNum as text)
set {oldTIDS, AppleScript's text item delimiters} to {AppleScript's
text item delimiters, {deci}}
set {part1, part2} to text items of theSignificand
set AppleScript's text item delimiters to oldTIDS
on error errMsg number errNum
-- no 'E' in the number!
return theNum as text
end try
set theExponent to theExponent as integer
set part2Length to count part2
if theExponent > part2Length then
set theZeros to ""
repeat (theExponent - part2Length) times
set theZeros to theZeros & "0"
end repeat
return part1 & part2 & theZeros
else if theExponent = part2Length then
return part1 & part2
else
return part1 & (text 1 thru theExponent of part2) & deci & text
(theExponent + 1) thru -1 of part2
end if
end normalise
In France it correctly returns:
(*101396,48*)
(*10139648*)
(*1013964800*)
On an English system, it correctly returns:
(*101396.48*)
(*10139648*)
(*1013964800*)
Yvan KOENIG (from FRANCE lundi 12 janvier 2009 18:29:06)
_______________________________________________
Do not post admin requests to the list. They will be ignored.
AppleScript-Users mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
Archives: http://lists.apple.com/archives/applescript-users
This email sent to email@hidden