How to display a float to 2 decimal places in JavaScript

2 Answers

0 votes
// JavaScript

function decimalPlaces(n)
{
    n = n.toFixed(2);
    document.write(n);
}

/*
run:

0.31
*/
// PHP

echo "<script> decimalPlaces(0.314159); </script>";


answered Jun 2, 2015 by avibootz
0 votes
// JavaScript

function decimalPlaces(n)
{
    n = n.toFixed(2);
    document.write(n);
}

/*
run:

0.36
*/
// PHP

echo "<script> decimalPlaces(0.35555); </script>";


answered Jun 2, 2015 by avibootz

Related questions

4 answers 651 views
4 answers 585 views
2 answers 513 views
4 answers 755 views
2 answers 268 views
1 answer 307 views
...