è un problema affrontato migliaia di volte su questo forum... ed è un problema di come la macchina computer rappresenta i numeri floating-point. Soluzione: decidi quanti decimali "esatti" vuoi e poi fai:
codice:
risultato = Math.round(risultato*Math.pow(10, numero_decimali_esatti))/Math.pow(10, numero_decimali_esatti);
//Esempio:
<html>
<head>
<script language="javascript">
function fixDecimals(i,j) {
return (Math.round(i*Math.pow(10,j))/Math.pow(10,j));
}
function test() {
var i = 1.6 - 1;
alert("Impreciso: "+i+"\n5 Cifre: "+fixDecimals(i,5));
}
</script>
</head>
<body onload="test();">
</body>
</html>