JavaScript Math.trunc() Examples [In-Depth Tutorial]


Written By - Olorunfemi Akinlua
Advertisement

Introduction

In JavaScript, there are many built-in methods that developers can use to make their code more efficient and effective. Math.trunc() is a built-in method in JavaScript that returns the integer part of a given number by removing any fractional digits. It is similar to the Math.floor() method, but Math.trunc() always rounds towards zero instead of rounding down.

In this article, we will discuss how to make use of Math.trunc() method across different operations

 

Absolute Values

Before we dive into Math.trunc(), it's important to understand the concept of absolute values. In mathematics, the absolute value of a number is its distance from zero on the number line. The typical definition you might have heard is the number value without signs, however, the distance of the number value from zero across the number line is a more accurate definition. For example, the absolute value of 5 is 5, and the absolute value of -5 is also 5.

 

Truncating Numbers

Truncation is the process of removing the decimal portion of a number and converting it to an integer. For example, if we truncate the number 3.14159, we get the integer value 3. Truncation is often used in situations where we only need the whole number portion of a calculation.

 

Using the Math.trunc() method in JavaScript

The Math.trunc() method is built into JavaScript and can be used to truncate decimal numbers to their integer values. Here's a look at the syntax for using this method:

Math.trunc(number);

The Math.trunc() method takes a single argument, which is the number we want to truncate. This number can be either positive or negative, and can include decimal places.

 

Example 1: Truncating Decimal Numbers

Let's start with a simple example. Suppose we have the decimal number 33.3414159 and we want to truncate it to its integer value. We can use the Math.trunc() method to accomplish this:

let number = 33.3414159;
let result = Math.trunc(number);
console.log(result);

Output

Advertisement
33

In this example, we declare a variable number and assign it the value of 3.14159. We then use the Math.trunc() method to truncate this number, and store the result in a new variable called result. Finally, we output the value of result to the console, which displays the integer value 3.

 

Example 2: Truncating a Negative Decimal Number

Now let's look at an example using a negative number. Suppose we have the decimal number -5.6789 and we want to truncate it to its integer value. We can use the Math.trunc() method to accomplish this:

let number = -5.6789;
let result = Math.trunc(number);
console.log(result);

Output

-5

In this example, we declare a variable number and assign it the value of -5.6789. We then use the Math.trunc() method to truncate this number, and store the result in a new variable called result. Finally, we output the value of result to the console, which displays the integer value -5.

 

Example 3: Truncating a String Representing a Number

In this example, the Math.trunc() method is used to truncate a string representing a number. The string is first converted to a number using the Number() function, then truncated using Math.trunc(). The result is the integer part of the input number.

let str = "155234.5678";
let result = Math.trunc(Number(str));
console.log(result);

Output

155234

 

Summary

The Math.trunc() method is a built-in method in JavaScript that allows developers to truncate decimal numbers to their integer values. This method can be very useful in many different programming scenarios, especially when dealing with complex mathematical calculations. By using this method, developers can ensure that their code is more efficient and effective.

 

References

Math.trunc() - JavaScript | MDN (mozilla.org)

Advertisement

 

Didn't find what you were looking for? Perform a quick search across GoLinuxCloud

If my articles on GoLinuxCloud has helped you, kindly consider buying me a coffee as a token of appreciation.

Buy GoLinuxCloud a Coffee

For any other feedbacks or questions you can either use the comments section or contact me form.

Thank You for your support!!

Leave a Comment