Skip to content

Instantly share code, notes, and snippets.

@CristalT
Created January 18, 2019 16:27
Show Gist options
  • Save CristalT/5b336cba180e76200bab0a53196b560e to your computer and use it in GitHub Desktop.
Save CristalT/5b336cba180e76200bab0a53196b560e to your computer and use it in GitHub Desktop.
// Two Ways To round a number at 2 decimals
const number = 19.5862365
// Way one
let result = Math.round(number * 100) / 100
console.log(result)
// Way two
result = parseFloat(number.toFixed(2)) // parseFloat() to keep number type
console.log(result)
// BONUS !!
result = new Intl.NumberFormat('en', {
minimumFractionDigits: 2,
maximumFractionDigits: 2
}).format(number)
console.log(result)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment