' Il metodo toFixed ()
' toFixed() restituisce una stringa, con il numero scritto con un numero specificato di decimali: 'Esempio 'var x = 9.656; 'x.toFixed(0); // returns 10 'x.toFixed(2); // returns 9.66 'x.toFixed(4); // returns 9.6560 'x.toFixed(6); // returns 9.656000 https://www.w3schools.com/js/tryit.asp?filename=tryjs_number_tofixed <!DOCTYPE html> <html> <body> <h2>JavaScript Number Methods</h2> <p>The toFixed() method rounds a number to a given number of digits.</p> <p>For working with money, toFixed(2) is perfect.</p> <p id="demo"></p> <script> var x = 9.656; document.getElementById("demo").innerHTML = x.toFixed(0) + "<br>" + x.toFixed(2) + "<br>" + x.toFixed(4) + "<br>" + x.toFixed(6); </script> </body> </html> 'Il metodo toFixed () arrotonda un numero a un dato numero di cifre. 'Per lavorare con i soldi, toFixed (2) e' perfetto. '10 '9.66 '9.6560 '9.656000 |