• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: How to convert number from exponential form?
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

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
  • Follow-Ups:
    • Re: How to convert number from exponential form?
      • From: Michelle Steiner <email@hidden>
References: 
 >How to convert number from exponential form? (From: Dave <email@hidden>)
 >Re: How to convert number from exponential form? (From: Barry Wainwright <email@hidden>)

  • Prev by Date: Re: Where to log lines go
  • Next by Date: Is there a way to delete original desktop folders with hard links to server folders?
  • Previous by thread: Re: How to convert number from exponential form?
  • Next by thread: Re: How to convert number from exponential form?
  • Index(es):
    • Date
    • Thread