Skip to content

Instantly share code, notes, and snippets.

@code-boxx
Created July 3, 2023 02:33
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 code-boxx/4c2b1712890d84dc65a7ff92006d0ccc to your computer and use it in GitHub Desktop.
Save code-boxx/4c2b1712890d84dc65a7ff92006d0ccc to your computer and use it in GitHub Desktop.
Javascript Round Off Numbers

JAVASCRIPT ROUND OFF NUMBER

https://code-boxx.com/javascript-round-up-down-decimal-places/

LICENSE

Copyright by Code Boxx

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

<!DOCTYPE html>
<html>
<head>
<title>Javascript Round</title>
<script>
// (A) NUMBERS
var numA = 11.4;
var numB = 12.5;
// (B) ROUND OFF
// < 0.5 - ROUND DOWN
// >= 0.5 - ROUND UP
console.log(Math.round(numA)); // 11
console.log(Math.round(numB)); // 13
// (C) CEILING - ALWAYS ROUND UP
console.log(Math.ceil(numA)); // 12
console.log(Math.ceil(numB)); // 13
// (D) FLOOR - ALWAYS ROUND DOWN
console.log(Math.floor(numA)); // 11
console.log(Math.floor(numB)); // 12
// (E) TRUNCATE - NO ROUND OFF, JUST DROP DECIMAL PLACES
console.log(Math.trunc(numA)); // 11
console.log(Math.trunc(numB)); // 12
</script>
</head>
<body></body>
</html>
<!DOCTYPE html>
<html>
<head>
<title>Javascript Round</title>
<script>
// (A) A BETTER ROUND OFF
// NUM : NUMBER TO BE ROUNDED
// PLACES : NUMBER OF DECIMAL PLACES
// MODE : NONE - ROUND, 0 - FLOOR, 1 - CEILING
function rounder (num, places, mode) {
// (A1) MULTIPLIER
var mult = parseInt("1" + "0".repeat(places));
num = num * mult;
// (A2) ROUND OFF
if (mode === 1) { num = Math.ceil(num); }
else if (mode === 0) { num = Math.floor(num); }
else { num = Math.round(num); }
// (A3) RETURN RESULTS
return num / mult;
}
// (B) TEST
var num = 1.2374;
// 2 DECIMALS ROUND - 1.24
console.log(rounder(num, 2));
// 3 DECIMALS FLOOR - 1.237
console.log(rounder(num, 3, 0));
// 3 DECIMALS CEILING - 1.238
console.log(rounder(num, 3, 1));
</script>
</head>
<body></body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment