logo

NJP

Round the Decimal Number

New article articles in ServiceNow Community · Sep 20, 2024 · article

Have you ever needed to round numbers in JavaScript? Here are a few handy functions you can use:

1. Math.round - This function is used to round a number to the nearest integer. If the fractional portion of the number is less than 0.5, it rounds down. If it's 0.5 or greater, it rounds up.

var round_number = Math.round(6.5);
round_number = 7 //result

2. Math.ceil(): This function always rounds a number up to the next largest integer or returns the integer itself if it's already an integer.

var ceil = Math.ceil(6.2);
ceil = 7 //result

3. Math.floor(): This function always rounds a number down to the next smallest integer or returns the integer itself if it's already an integer.

var floor_number = Math.floor(6.9);
floor_number = 6 //result

These functions are quite handy when you need precise control over rounding numbers in JavaScript!

View original source

https://www.servicenow.com/community/developer-blog/round-the-decimal-number/ba-p/3051768