![]() | |
![]() |
| | Thread Tools | Display Modes |
#11
| |||
| |||
|
#12
| |||
| |||
|
#13
| |||
| |||
|
|
SAM wrote on 27 okt 2008 in comp.lang.javascript: avascript calculate on base 64 (I think, or something like that) and sometimes it can't give the exactly number such as : 1.0000000009 instead of 1 You could try by using your variables multiplied by 1000 or 100000 and finally get back the roundness of the result divided by the same coefficient Such division could sometimes introduce the same kind of error, methinks. Better use regex do a string manipulation imitating such division: var aNumber = 1.2345 var resultString = (aNumber*1000+.5).toString().replace(/(\d\d\d)$/,'.$1') |
#14
| |||
| |||
|
|
Better use regex do a string manipulation imitating such division: var aNumber = 1.2345 var resultString = (aNumber*1000+.5).toString().replace(/(\d\d\d)$/,'.$ 1') var aNumber = 1.2345678; var resultString = (aNumber*1000+.5).toString().replace(/(\d\d\d)$/,'. $1'); alert(resultString); // 1235.0.678 |
#15
| |||
| |||
|
|
sasuke wrote on 30 okt 2008 in comp.lang.javascript: Better use regex do a string manipulation imitating such division: var aNumber = 1.2345 var resultString = (aNumber*1000+.5).toString().replace(/(\d\d\d) $/,'.$ 1') var aNumber = 1.2345678; var resultString = (aNumber*1000+.5).toString().replace(/(\d\d\d)$/,'. $1'); alert(resultString); // 1235.0.678 You are right, should be[, only where the absolute value is above 1]: var aNumber = 1.2345678; var resultString = Math.floor(aNumber*1000+.5).toString().replace(/(\d\d\d)$/,'.$1'); alert(resultString); // 1.235 |
#16
| |||
| |||
|
|
Wow, not that easy in regex, try: script type='text/javascript' function round3dec(x) { x = Math.floor(x*1000+.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.2345678) ); alert( round3dec(-21.2345678) ); alert( round3dec(0.0025555) ); alert( round3dec(-0.0025555) ); /script |
#17
| |||
| |||
|
|
On 2008-10-30 21:59, Evertjan. wrote: Wow, not that easy in regex, try: script type='text/javascript' function round3dec(x) { * x = Math.floor(x*1000+.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.2345678) ); alert( round3dec(-21.2345678) ); alert( round3dec(0.0025555) ); alert( round3dec(-0.0025555) ); /script (1000000000000000000).toFixed(3) * -> 1000000000000000000.000 round3dec(1000000000000000000) * -> 1e+21.00001e+21 :-) |
#18
| |||
| |||
|
|
LOL, that's a nice one. Moral of the story: Use built-in functions whenever possible. ;-) |
#19
| |||
| |||
|
|
On Oct 31, 2:57*am, Conrad Lender <crlen... (AT) yahoo (DOT) com> wrote: On 2008-10-30 21:59, Evertjan. wrote: Wow, not that easy in regex, try: script type='text/javascript' function round3dec(x) { * x = Math.floor(x*1000+.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.2345678) ); alert( round3dec(-21.2345678) ); alert( round3dec(0.0025555) ); alert( round3dec(-0.0025555) ); /script (1000000000000000000).toFixed(3) * -> 1000000000000000000.000 round3dec(1000000000000000000) * -> 1e+21.00001e+21 :-) LOL, that's a nice one. Moral of the story: Use built-in functions whenever possible. ;-) |
![]() |
| Thread Tools | |
| Display Modes | |
| |