Skip to content

Instantly share code, notes, and snippets.

@pjfranzini
Created January 4, 2014 00:29
Show Gist options
  • Save pjfranzini/8249588 to your computer and use it in GitHub Desktop.
Save pjfranzini/8249588 to your computer and use it in GitHub Desktop.
round, ceil, floor, etc
# same only can leave off parens and ;
Math.round(2.8); // round to nearest integer => 3
Math.floor(2.8); // largest integer less or equal to number => 2
Math.ceil(3.2); // smallest integer greater or equal to number => 4
Math.trunc(2.8); // remove fractional digits => 2 ; experimental
Math.trunc(-2.8); // -2
Math.floor(-2.8); // -3
num.toFixed(2); // round to two decimal places
Math.round(num * 100) / 100 // another way to round to two decimal places
# use .ceil, .floor, .round, .truncate
# .round(n) rounds to n decimal digits
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment