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 653 views
4 answers 588 views
2 answers 514 views
4 answers 756 views
2 answers 270 views
1 answer 308 views
...