Rounding values in Javascript

Rounding values in Javascript

Rounding values has been an issue for some developers because they seem confused on what method to use actually when involved in calculations.

I'm going to fully explain these methods; their function and how they work with positive and negative values. Rounding of values can be done in many languages but I would be focusing on Javascript.

Why do we round values in Javascript?

  • Rounding is all about simplifying the values.

  • Rounded values are easier to work with.

  • It makes them easier and simpler to use and understand.

Rounding values is actually divided into two; either you round the value up or you round it down. A quick definition of these terms:

  • Rounding up: If the first value after the decimal point is 5 or more, one(1) is added to the number before the decimal number.

  • Rounding down: If the first value after the decimal point is lesser than 5, zero(0) is added to the number before the decimal number.

Since we have defined these terms, let's get right into the methods that we use in rounding up or down values.

1 The round method (Math.round)

This method rounds up any value to an integer (whole number) if the first value after the decimal point is greater than 5 by adding 1 to the value before the decimal point and the method rounds down any value to an integer if first value after the decimal point is lesser than 5 by adding 0 (zero) to the value before the decimal point.

//The integer after the decimal point is equal or greater than 5
console.log(Math.round(23.5));//Returns 24
//The integer after the decimal point is lesser than 5
console.log(Math.round(23.4));//Returns 23

There is an exception. This method always rounds up negative values either if the first value after the decimal point is lesser or greater than 5

console.log(Math.round(-23.5));//Returns -23
console.log(Math.round(-23.4));//Returns -23

2 The trunc method (Math.trunc)

This method returns only the integer. It removes the decimal part of the value. The method is advisable to use when you want to remove all fractional digits.

console.log(Math.trunc(23.95));//Returns 23
console.log(Math.trunc(23.4));//Returns 23

It also works the same way for negative values.

console.log(Math.trunc(-23.1));//Returns -23
console.log(Math.trunc(-23.9));///Returns -23

3 The floor method (Math.floor)

This method round positive values down to the nearest integer either if the first value after the decimal point is greater or lesser than 5.

console.log(Math.floor(2.9));//Returns 2
console.log(Math.floor(2.3));//Returns 2

This method also works the same on negative values. It rounds the value down.

console.log(Math.ceil(-2.5));//Returns -2
console.log(Math.ceil(-2.3));//Returns -2

Conclusion

That's all about rounding values in Javascript. You just have to use it frequently for better understanding. If you have any comment or question, do not hestitate to ask. Support me by sharing my article to anyone that needs it. Be consistent and keep building!!!

Did you find this article valuable?

Support Mustafa Umar Omotayo by becoming a sponsor. Any amount is appreciated!