Yahoo Answers is shutting down on May 4th, 2021 (Eastern Time) and beginning April 20th, 2021 (Eastern Time) the Yahoo Answers website will be in read-only mode. There will be no changes to other Yahoo properties or services, or your Yahoo account. You can find more information about the Yahoo Answers shutdown and how to download your data on this help page.

How do I control amount of numbers after the decimal point in javascript?

I just started in JS this month (Nov. 2010) and would like to be able to have the ability to show as few or as many places to the right of the decimal point as I want, especially in divsions.

This would be printed to screen using document.write.

If it can print to screen using another command feel free to include it if a beginner can understand it.

Thanks!

2 Answers

Relevance
  • Matt D
    Lv 4
    1 decade ago
    Favorite Answer

    use Number.toFixed().

    For example (1.0/3).toFixed(2) should evaluate to "0.33"

  • 1 decade ago

    You can try this program from

    W3Schools, it's in the back pages of the JS tutor and kinda obscure, so use the link below too!

    <script type="text/javascript">

    var num = new Number(13.3714);

    document.write(num.toFixed()+"<br />");

    document.write(num.toFixed(1)+"<br />");

    document.write(num.toFixed(3)+"<br />");

    document.write(num.toFixed(10));

    </script>

    I hope it works for what you want!

    Kat

Still have questions? Get your answers by asking now.