Skip to content
the poor coder

how to cut off decimals in javascript

Poor Man
Published
Reading time
2 min read
On this page

How to cut off decimals in JavaScript#

Dealing with decimal points can be a bit tricky in JavaScript, especially when we want to display numbers without any decimal points. Here, we will discuss how to cut off decimals in JavaScript.

The toFixed() Method#

The toFixed() method is used to format a number with a specific number of digits after the decimal point. It returns a string representation of the number with the specified number of digits after the decimal point. Here's an example:

Text

let num = 3.14159;
let n = num.toFixed(2);
console.log(n); // Output: "3.14"

In the above example, we have defined a variable 'num' with a value of 3.14159. Then we have used the toFixed() method to format the number with two digits after the decimal point. The output will be "3.14".

The Math.trunc() Method#

The Math.trunc() method is used to remove the decimal part of a number and return the integer part. Here's an example:

Text

let num = 3.14159;
let n = Math.trunc(num);
console.log(n); // Output: 3

In the above example, we have defined a variable 'num' with a value of 3.14159. Then we have used the Math.trunc() method to remove the decimal part and return the integer part. The output will be 3.

The parseInt() Method#

The parseInt() method is used to parse a string and return an integer. If we pass a decimal number as a string to the parseInt() method, it will return the integer part of the number. Here's an example:

Text

let num = "3.14159";
let n = parseInt(num);
console.log(n); // Output: 3

In the above example, we have defined a variable 'num' with a value of "3.14159" as a string. Then we have used the parseInt() method to parse the string and return an integer. The output will be 3.

Conclusion#

In conclusion, we have discussed three different ways to cut off decimals in JavaScript. The toFixed() method is used to format a number with a specific number of digits after the decimal point, the Math.trunc() method is used to remove the decimal part of a number and return the integer part, and the parseInt() method is used to parse a string and return an integer. Depending on our use case, we can choose any of these methods to cut off decimals in JavaScript.

Keep going

2023

how to sort an array

How to Sort an Array If you have an array and you want to order its elements in a specific way, you need to use a sorting algorithm. There are several sorting algorithms available, but two of the most commonly used are…

javascript2 min read
2023

jquery mobile or desktop

jQuery Mobile or Desktop? As a web developer, I have had some experience with both jQuery Mobile and jQuery Desktop. Both frameworks have their pros and cons, and which one to use really depends on the specific project…

javascript1 min read
2023

node.js folder structure

Node.js Folder Structure If you are new to Node.js, it can be daunting to create a folder structure that will work for your project. The structure you choose will depend on the size and complexity of your project. Here…

javascript1 min read
ESC