Round in JavaScript

Best way to round secure in all browsers.


Libraries

Code Snippet
function round(num, places) {    
    return parseFloat((Math.round(num + "e+" + places.toString())  + "e-" + places.toString()));
}


Implementation

Use the code above like<code>round(1.052, 2) // to round for 2 places. (returns 1.05) round(1.052, 3) // to round for 3 places. (returns 1.052) round(1.052, 1) // to round for 1 places. (returns 1.1)</code> You optionally can also use<code>1.052.toFixed(2)</code>but note that is not supported in older browsers.





Comments
  • mode_editWrite a comment



  • forumComments




Embed Code

We provide special containers which include the full snippet and a link to this site. So for example when you make a tutorial and you are using code from this site, you can use one of the following snippet embeddings:

Load Preview Get Code Close Preview







    Share
    Rating
    thumb_up 0 thumb_down
    Author
    Thorolus

    I'm the head developer and co-founder of GlovilGames Studios (glovilgames.com) and other platforms GlovilGames created, like vsnippets.com, worth-calculator.net, python-obfuscator.com and some mobile apps and games.

    Tags
    Basics



    Informations

    posted on 2016-09-22 17:27:34

    viewed 45 times

    snippet's UUID is 57e3f8664ec4e

    Similar Snippets