Scientific E notation: How to get it
FlexibleLearning.com
admin at FlexibleLearning.com
Mon Sep 23 05:06:36 EDT 2013
I needed to convert decimals to E notation and as it took me a while, I
thought it might be useful to others.
If you have a shorter alternative or find any glitches, please post!
on mouseUp
put fld "in" into tNum
put fld "NumberFormat" into tNumberFormat --| to specify output precision.
Normally 0.00
put E(tNum,tNumberFormat) into fld "out"
end mouseUp
function E pNum,pNF
local tSign,tEsign,tInt,tDec,n,tBase,tE
if pNum is not a number then return pNum --| or "NaN" or "error"
if pNum <0 then put "-" into tSign
--
if abs(pNum) >=1 then put "+" into tEsign
else put "-" into tEsign
if char 1 of pNum ="-" then delete char 1 of pNum
--
set the itemDel to "."
put item 1 of pNum into tInt; add 0 to tInt
put item 2 of pNum into tDec
--
if tEsign ="+" then
put len(tInt)-1 into tE
put char 1 of tInt into tBase
put char 2 to -1 of (tInt & tDec) into tDec
else
repeat with n=1 to len(tDec)
if char n of tDec >0 then exit repeat
end repeat
put n into tE
put char n of tDec into tBase
put char n+1 to -1 of tDec into tDec
end if
if tBase + tDec =0 then return "0.00E+00" --| standard format case
exception
--
set the numberFormat to pNF
put (tBase&"."&tDec)+0 &"" into n --| As string
--
set the numberFormat to "00"
return tSign &n& "E"& tESign &tE
end E
Hugh Senior
FLCo
More information about the use-livecode
mailing list