How Is Infinity converted to Number in JavaScript?



A simple wrapper object is the number. Numerous methods and constants are available when using the Number function Object() { [native code] }. The Number() method may be used to transform values of different kinds into numbers. The IEEE 754 double-precision 64-bit binary value type is used in JavaScript. This indicates that it can represent fractional values, but the amount of data it can hold is constrained.

Arithmetic is vulnerable to rounding, and a number only retains roughly 17 decimal places of precision. A Number may carry a maximum value of around 1.8E308. Higher values are substituted with the unique Number Constant Infinity. A floating-point value, not an integer, is what is meant by a number literal in JavaScript code. There is no distinct integer type in use today.

In this tutorial, we will learn how infinity is converted to Boolean in JavaScript. Furthermore, we have different methods to achieve our goal in this tutorial.

Using the Number() Method

This approach will see how to use the Number object to convert infinity values to Numbers. You may display a numeric value using the JavaScript number object, which might be a floating point or integer. The IEEE standard is followed by the JavaScript number object for representing floating-point numbers. NaN is returned if the value cannot be transformed.

Syntax

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

var n=new Number( value ); 

Parameters

  • value − The parameter passed to the Number object whose Number value will be determined

Example

The below example demonstrates how to use the Number() object for converting infinity values to Number. Here, we declare two objects myVal and val which take two values infinity and –infinity. Both these are infinity values and return infinity when passed through the Number object.

<html> <body> <h2>Convert Infinity to Number</h2> <script> var myVal = Infinity; document.write("Number of +infinity: " + Number(myVal)); document.write("<br>") var val = -Infinity; document.write("Number of -infinity: " + Number(val)); </script> </body> </html>

In the above output, users can see that the value of +infinity in Number is the same as +infinity and that of –infinity is the same as -infinity. These values have been obtained from the Number object.

Using the Bitwise NOT Operator

The bits of its operand is inverted by the bitwise NOT operator (~). The operand is transformed into a 32-bit signed integer, much as other bitwise operators. The operand is represented as a set of bits and transformed to a 32-bit signed integer (zeroes and ones). The most important bits of numbers with more than 32 bits are eliminated.

Syntax

We can follow the syntax below to convert the Bitwise Not (~) operator.

let r = ~a;

Example

In the below example, we have used the Bitwise NOT operator to convert infinity to a number data type. The bitwise NOT operator is used to invert the bits of an operand and then the Number() object changes the value of ((0-18)/0*100) which turns to infinity. This value is being passed to the toFixed() method so that only 2 significant digits of the value are being passed and hence the variable stores a value of 0, which is a number.

<html> <body> <h2>Convert Infinity to Number</h2> <script> var result = ~~Number((((0-18)/0)*100).toFixed(2)); document.write("Data type: "); document.write(typeof(result)); document.write("<br>"); document.write(" Infinity to Number: "+result); </script> </body> </html>

In the above output, we have seen how an infinity value obtained by calculation is converted to a number value. The data type of the result has been obtained as Number.

Explicit Type Conversion

The process to convert data from one type to another is known as type conversion in programming. Depending on your needs, you may also convert between other data types. Explicit type conversion is the manual type conversion you do.

Syntax

if (result == Number.POSITIVE_INFINITY || result == Number.NEGATIVE_INFINITY)
{
   result=0;
}

Example

In this example, we will see how the infinity value is being converted to a number using explicit type conversion. The variable result stores a calculation which results in infinity. Using the if-condition we check whether that result is positive infinity or negative infinity. Then that value is getting type cast to an integer 0. The data type of 0 is found as a Number and that value along with the result is printed on the user’s screen.

<html> <body> <h2>Convert Infinity to Number</h2> <script> var result = Number((((0-18)/0)*100).toFixed(2)); if (result == Number.POSITIVE_INFINITY || result == Number.NEGATIVE_INFINITY) { result=0; document.write("Data type: "); document.write(typeof(result)); document.write("<br>"); document.write(" Infinity to Number: "+ result); } </script> </body> </html>

In the above example, we have seen how an infinity value obtained by calculation is converted to a number value. The data type of the number is also getting obtained.

This tutorial used three approaches to convert an infinity value to a number data type. A global object's attribute, or a variable with a global scope, is infinity. JavaScript uses the basic data type called Number for positive or negative integer, float, binary, octal, hexadecimal, and exponential values.

The first method uses a simple Number() object to convert the value of positive infinity and negative infinity to number data types. The second method uses the bitwise NOT operator to convert an infinity calculation to a number data type of 0. The third method showcases how explicit type conversion can be implemented on the calculated infinity result.


Advertisements