Groups | Blog | Home
all groups > flash actionscript > july 2005 >

flash actionscript : Scientific Notation in Flash


D. Ziv
7/20/2005 6:05:34 PM
You can write your own customized function or use Xinware XData Float
class to set precision.

DZ @ Xinware

[quoted text, click to view]
D. Ziv
7/20/2005 7:40:37 PM
Truce.

Peace lover, ZD @ Xinware

barcs16
7/20/2005 9:28:47 PM
I am trying to work out a physics formula in flash, the equations using
notations like 6.673e-8 which flash can compute just fine, but i was wondering
if there is a way to round this answer:
9.03487346928e-8 to read 9.03e-8
If anyone knows how to do this I would appreciate the help!
Thanks
barcs16
7/21/2005 12:00:00 AM
I flash therefore I am
7/21/2005 12:00:00 AM
There is a function you can have for this that you can download at:

http://sierrabravocharlie.com/experimental/exponential_notation.fla

Jeckyl
7/21/2005 12:00:00 AM
[quoted text, click to view]

If you want to pay several hundred dollars for the privelege :)

Here is a function to do it I just wrote for you .. hope it works ok:

function roundOffDecimals(val, nDecMax) {
// find any negative size, remember and remove (add back later)
var isNeg = val < 0;
var sSign = '';
if (isNeg) {
val = -val;
sSign = '-';
}
// convert to a string
var sVal = String(val);
// if it is short enough, just return it
if (sVal.length < nDecMax) return sSign+sVal;
// find any exponent part, remember and remove (add back later)
var ePos = sVal.indexOf('e',0);
if (ePos >= 0 && ePos < nDecMax) return sSign+sVal;
var sEPart = '';
if (ePos >= 0) {
sEPart = sVal.substr(ePos,sVal.length-ePos);
sVal = sVal.substr(0,ePos);
}
// find any decimal point, remember and remove (add back later)
var dotPos = sVal.indexOf('.',0);
if (dotPos < 0) return sSign+sVal+sEPart;
if (dotPos + nDecMax + 1 >= sVal.length) return sSign+sVal+sEPart;
sVal = sVal.split('.').join('');
// do rounding and then chop off extra digits at the end
sVal = String(parseInt(sVal.substr(0,dotPos+nDecMax+1),10)+5);
sVal = sVal.substr(0,dotPos)+'.'+sVal.substr(dotPos,nDecMax);
// all done
return sSign+sVal+sEPart;
}

some testing...
trace(roundOffDecimals(1,2));
trace(roundOffDecimals(1.2,2));
trace(roundOffDecimals(12.34,2));
trace(roundOffDecimals(123.456,2));
trace(roundOffDecimals(654.321,2));
trace(roundOffDecimals(1.23456e20,2));
trace(roundOffDecimals(9.87654e20,2));
trace(roundOffDecimals(6.54321e-20,2));


--

[quoted text, click to view]

AddThis Social Bookmark Button