Advertisement
dwhitzzz

Formatting a number with exactly two decimals in JavaScript

Jul 22nd, 2016
154
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. Formatting a number with exactly two decimals in JavaScript
  2.  
  3. One way to be 100% sure that you get a number with 2 decimals:
  4.  
  5. (Math.round(num*100)/100).toFixed(2)
  6. If this causes rounding errors, you can use the following as James has explained in his comment:
  7.  
  8. (Math.round((num * 1000)/10)/100).toFixed(2)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement