Skip to content

Instantly share code, notes, and snippets.

@Ahmdrza
Last active May 8, 2019 09:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Ahmdrza/7b5cedc357d7ae345fbdf4049bfca86b to your computer and use it in GitHub Desktop.
Save Ahmdrza/7b5cedc357d7ae345fbdf4049bfca86b to your computer and use it in GitHub Desktop.
This code converts any decimal number to max 2 decimal places.
//https://stackoverflow.com/questions/11832914/round-to-at-most-2-decimal-places-only-if-necessary
//For those of you that don't get it this technique is called scaling. Basically what the answer does here is bring two figures across the decimal point turning the figure into a whole number to avoid all the crazy floating point issues, round that and then translate it back into what it was before by dividing by 100 and you have your answer to 2dp.
Math.round((num + 0.00001) * 100) / 100
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment