[quoted text, click to view] > You can write your own customized function or use Xinware XData Float
> class to set precision.
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] "D. Ziv" <dz@xinware.com> wrote in message
news:dbmhni$ahc$1@forums.macromedia.com...
> You can write your own customized function or use Xinware XData Float
> class to set precision.
>
> DZ @ Xinware
>
> barcs16 wrote:
>
>>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
>>
>>