Created
January 4, 2014 00:29
-
-
Save pjfranzini/8249588 to your computer and use it in GitHub Desktop.
round, ceil, floor, etc
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# same only can leave off parens and ; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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