convert a float number to a whole number in JavaScript

How do I convert a float number to a whole number in JavaScript?

In this tutorial, we will learn to convert a float number to a whole number in JavaScript. The whole number is the number that doesn’t contain the fractional part. For example, 5.22 is the float number containing the fractional part, and the whole number doesn’t contain a decimal part. So, our goal in this tutorial is to remove the fractional part from the number.

We can have various methods to cut out the decimal part from the float number to make it whole, and some of them we have discussed here.

  • Using the Math.trunc() and Math.round() Methods
  • Using the parseInt() Method
  • Using the Bitwise OR (|) Operator
  • Subtracting the Fractional Part from the Float Number

Using the Math.trunc() and Math.round() Methods

The Math.trunc() method cuts the fractional part from the float numbers and makes it the whole number.

The Math.round() method rounds the fractional number towards the nearest whole number. For example, if the fractional part is less than 0.5, it returns the nearest smaller number than the current number, and if the fractional part is greater than 0.5, it returns the nearest greater number than the float number.

Syntax

Users can follow the below syntax to use the Math.trunc() and Math.round() method.

let result = Math.trunc ( number ); let result = Math.round ( number );

Parameters

Example

In this example, we will use the Math.trunc() and Math.round() method to convert the float number to the whole number.

    

Convert a float number to a whole number in JavaScript

Converting 42.99 to whole number using Math.trunc() method:

Converting 42.99 to whole number using Math.round() method: let output1 = document.getElementById("output1"); let output2 = document.getElementById("output2"); let number = 42.99; let result = Math.trunc(number); output1.innerHTML = result; number = 42.99; result = Math.round(number); output2.innerHTML = result;

In the above output users can see that Math.trunc() method has removed the decimal part and Math.round() method has rounded the number to nearest integer.

Using the parseInt() Method

In this approach, we will use the parseInt() method. The parseInt() method extracts the whole number from the float number or string in JavaScript. The other easiest solution is to get the whole number part from the float number.

Читайте также:  Java случайным образом массив

Syntax

Users can follow the below syntax to use the parseInt() method.

let result = parseInt( number );

Example

In the below example, we have used the parseInt() method to convert the float number to whole number.

  

Convert a float number to a whole number in JavaScript

Converting 22.19234 to whole number using parseInt() method:

let outputDiv = document.getElementById("outputDiv"); let number = 22.19234; let res = parseInt(number); outputDiv.innerHTML = res;

Using the Bitwise OR (|) Operator

When we perform the Bitwise OR (|) operation of 0 with the float number, it removes the decimal part from the current number and converts it to the whole number. Similarly, users can also use other Bitwise operators, such as the Double Not operator (~~), Right shift, and left shift operators.

Syntax

let result = number | 0; // number is the float number

Example

In the given example, we are performing the Bitwise OR operation of 209.54321 with the 0 and converting it into the whole number. After that, we have rendered the output using JavaScript.

  

Convert a float number to a whole number in JavaScript

Converting 209.54321 to whole number using Bitwise OR (|) operator:

let outputDiv = document.getElementById("outputDiv"); let number = 209.54321; let res = number | 0; outputDiv.innerHTML = res;

Subtracting the Fractional Part from the Float Number

This method will find the fractional part of the float number. To find the fractional part of the float number, we just need to take the modulo of the number with 1. Next, we will subtract the fractional part from the number.

Syntax

let fractionalPart = number%1; let result = number – fractionalPart;

Example

In the below example, we are finding the fractional part of the number by taking the modulo of number with 1. After that, we are subtracting the fractional part from the number to remove decimal part from it.

  

Converting 856.24657 to whole number by removing the fractional part:

let outputDiv = document.getElementById("outputDiv"); let number = 856.24657; let fractionalPart = number % 1; let result = number - fractionalPart; outputDiv.innerHTML = result;

Conclusion

We have discussed the four approaches to converting the float number to a whole number. The second approach is the easiest one as it parses the integer only from the float number. However, the user should use the third approach as it is the fastest one.

Читайте также:  Php using functions with variables

Источник

How to Convert a Float Number to Int in JavaScript

The conversion of data types from one to another is referred to as the key operation in JavaScript programming. JavaScript has various built-in methods to convert a data type to another data type. For instance,the Math object supports various methods, including Math.ceil(), Math.floor(), Math.round(), and Math.trunc() to convert the float number to an integer. These methods are used to convert a float number to an integer.

In this guide, we have used various math object methods and other JavaScript methods to convert float numbers to integers. Here is the content of this guide:

Method 1: Math.ceil()

JavaScript provides a ceil() method by utilizing the Math object. The method returns the next higher integer number from the floating point.

Syntax

The parameter val represents the floating value of any number.

Example

In the above code, “25.1” and “-25.1” are the two floating point numbers that are passed to the Math.ceil() method.

Output

The output shows that “25.1” is converted to “26” (as the nearest higher integer value of “25.1”) and “-25.1” to “-25” (nearest higher integer value of “-25.1”).

Method 2: Math.round()

The method round() is employed to round the fractional number to the closest number. The syntax of the Math.round() method is as follows:

Syntax

The parameter val denotes the floating number to convert into an integer.

Example

In the above code, Math.round() methods are utilized by passing two float numbers, “25.13” and “13.523”.

Output

It is observed that “25.13” is converted to “25” (as the value after the fractional point is less than 5). Similarly, the value “13.523” is converted to “14” (because the value after the fractional point is greater than 5).

Method 3: Math.trunc()

The trunc() method removes the fractional part of the passed number and returns the whole number only. The syntax of the Math.trunc() method is given below:

Syntax

The parameter val represents the floating value of any number.

Example

In this code, Math.trunc() methods are employed by passing two float numbers, “15.1” and “-175.1”.

Output

The output removes the fractional part of the floating number (.1) and returns the integer “15” and “-175” in the console window.

Method 4: Math.parselnt()

The parseInt() method provides a simple solution to convert the float number to an integer. It retrieves the integer number from the fractional number and returns the output as an integer number.

Syntax

The val parameter means the floating number to convert into an integer.

Читайте также:  Стиль для ячейки css

Example

The parseInt() method is utilized by taking input “24.5” and “-24.5” and displaying the output using the console.log() method.

Output

The output shows that “24.5” is converted to “24” (by removing floating numbers). Similarly, “-24.5” to “-24” (by removing floating numbers) in the console window.

Method 5: Bitwise Or Operation

The Bitwise Or operation allows the conversion of the float number to an integer by removing the decimal part of the current number. The syntax of the Bitwise Or operator is as follows:

Syntax

The num represents the float number in JavaScript.

Example

In this code, “23.5” and “-25.5” are passed as floating numbers to be converted into integers.

Output

The output shows “23.5” to “23” and “-25.5” to “-25” by removing the fractional number in the console window.

Method 6: Math.floor()

The method provides a rounding-off facility to the downside for the passing floating number. The syntax is given below.

Syntax

The parameter val describes the floating number to convert into an integer.

Example

An example is considered using the floor() method by passing different values.

The Math.floor() method is employed by passing floating numbers “25.1” and “-25.1” to convert into integers.

Output

The output shows that the Math.floor() method has converted (by removing the fractional part) the “25.1” to “25” and “-25.1” to “-26”.

Method 7: toFixed()

Another method is adapted to convert the floating number to an integer using toFixed() in JavaScript.

In this case, the number is rounded off to the closest integer value. The syntax is provided below:

Syntax

In the above syntax, the dig specifies the number of digits for the decimal part.

Example

In this code, the toFixed() method is utilized to convert the floating number “6546.1453” to integers.

Output

The output returns the integer number “6546” (the closest integer number) from “6546.1453” in the console window.

Conclusion

In JavaScript, Math.ceil(), Math.floor(), Math.round(), Math.trunc() methods are employed to convert the float number to integer. All these methods take a floating point as an input and return an integer number. Moreover, parseInt(), bitwise Or operation, and toFixed() methods are utilized to remove the decimal numbers of floating numbers. In this article, you have learned how to convert a float number to an integer using numerous JavaScript methods.

About the author

Syed Minhal Abbas

I hold a master’s degree in computer science and work as an academic researcher. I am eager to read about new technologies and share them with the rest of the world.

Источник

Оцените статью