Round and Truncate Numbers after the Decimal Point with JavaScript

Web Dev
Sponsored links

In this article, I will introduce how to round and truncate numbers after the decimal point with JavaScript.

Math.ceil()

The Math.ceil() rounds up the value specified in the argument after the decimal point and returns the smallest integer greater than or equal to the value specified in the argument.

JavaScript

 Math.ceil(3.45);
//result:4

Math.ceil(-3.45);
//result:-3
Sponsored links

Math.floor()

The Math.floor() truncates the value specified in the argument after the decimal point and returns the largest integer less than or equal to the value specified in the argument.

JavaScript

Math.floor(3.45);
//result:3

Math.floor(-3.45);
//result:-4
Sponsored links

Math.round()

The Math.round() rounds the value specified in the argument and returns the closest integer.

JavaScript

Math.round(3.45);
//result:3

Math.round(3.56);
//result:4

Math.round(-3.45);
//result:-3

Math.round(-3.56);
//result:-4
Sponsored links

Math.trunc()

The Math.trunc() excludes the decimal point of the value specified in the argument and returns only the integer part.

JavaScript

Math.trunc(3.45);
//result:3

Math.trunc(-3.45);
//result:-3