Re: JavaScript Math vs Excel
On Oct 31, 2:57 am, Conrad Lender <crlen...@yahoo .comwrote:
LOL, that's a nice one. Moral of the story: Use built-in functions
whenever possible. ;-)
../sasuke
On Oct 31, 2:57 am, Conrad Lender <crlen...@yahoo .comwrote:
On 2008-10-30 21:59, Evertjan. wrote:
>
>
>
>
>
>
>
>
(10000000000000 00000).toFixed( 3)
-100000000000000 0000.000
>
round3dec(10000 00000000000000)
-1e+21.00001e+21
>
:-)
>
>
>
Wow, not that easy in regex, try:
<script type='text/javascript'>
function round3dec(x) {
x = Math.floor(x*10 00+.5)+'';
var s = x.replace(/(^\-?).*/,'$1');
x = x.replace(/^\-?/,'0000');
return s + (1*x.replace(/\d\d\d$/,'')) + '.' +
x.replace(/-?\d*(\d\d\d)$/,'$1');
};
x = Math.floor(x*10 00+.5)+'';
var s = x.replace(/(^\-?).*/,'$1');
x = x.replace(/^\-?/,'0000');
return s + (1*x.replace(/\d\d\d$/,'')) + '.' +
x.replace(/-?\d*(\d\d\d)$/,'$1');
};
alert( round3dec(21.23 45678) );
alert( round3dec(-21.2345678) );
alert( round3dec(0.002 5555) );
alert( round3dec(-0.0025555) );
alert( round3dec(-21.2345678) );
alert( round3dec(0.002 5555) );
alert( round3dec(-0.0025555) );
</script>
(10000000000000 00000).toFixed( 3)
-100000000000000 0000.000
>
round3dec(10000 00000000000000)
-1e+21.00001e+21
>
:-)
whenever possible. ;-)
../sasuke
Comment