Round and Truncate Numbers after the Decimal Point with JavaScript

01/13/2022

Contents

In this article, you will learn 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

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

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

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