Codesnipp.it Social Code Sharing

Jevin

Javascript function to return numbers in specified decimal places.

by Jevin on Dec 29, 2011

<script> function formatNumber(number, decimalPlaces) { var coeff = Math.pow(10, decimalPlaces); return Math.round(number * coeff) / coeff; } var pi = 3.14159265; document.write('Though the value of PI is actually ' + pi + ', most people only remeber it to be ' + formatNumber(pi, 2) + '.'); </script>

Can't see the comments? Please login first :)