Before we start solving mysteries about conversions, we should know the difference between int and float types.
Table of Contents
- What is The Difference Between Int And Float?
- Numerical Data Types of Programming Languages
What is The Difference Between Int And Float?
An int is a number without a decimal point, also known as a whole number, on the other hand, a float is a number with a decimal point, also known as a real number. For example, 4 is an integer, and 4.20 is a floating point.
Numerical Data Types of Programming Languages
Following are some number data types that most programming languages have in common:
|Type||Store||No. of Bits|
|Type||Store||No. of Bits|
|Number||Whole numbers and Real numbers||64|
console.log("Example of int and float"); // declare an integer number let x= 20; // declare a float number let y=30.15; console.log("x=",x); console.log("y=",y);
Example of int and float x= 20 y= 30.15
When we are handling data of different types and want to present data in a specific type, for example, if a user enters data in string type or integer type, and we want to present it in float data type, then we have to use some conversion method to do so.
We have to use the following two methods to convert int into a float
Now see the detail of both methods on how we can convert an integer into a float:
Method 1: Use of toFixed()
We can use the toFixed method to convert a number into a string with fixed point notation. For example:
console.log("Example of toFixed method"); // declare an integer number let x= 20; console.log("Value of x=",x); // we pass number digits toFixed method // this example round up till two decimal points console.log("Value of x with toFixed()=",x.toFixed(2));
Example of toFixed method Value of x= 20 Value of x with toFixed()= 20.00
The toFixed() method takes a digit 0 to 100 from us and returns a string with a specified number of digits after the decimal point. If we pass a digit more than 100 or less than 0 it will cause a RangeError.
Method 2: Use of parseFloat()
The parseFloat method takes a string as an argument and parses it into a float(real number). If the passed string is not a number or the first character is not a number, this function will return NaN(not a number). In the following example, we use the typeof operator to check which data type is returned by the parseFloat method:
console.log("Example of parseFloat method"); // declare a string containing a floating point number let x= "20.15"; console.log("type of x=",typeof x); // we pass a string to the parseFloat method console.log("type of value returned by parseFloat()=",typeof parseFloat(x));
Example of parseFloat method type of x= string type of value returned by parseFloat()= number
We can convert int into a float with the help of toFixed and parseFloat methods. The toFixed method return string later, which we convert into float. Now understand this concept with the help of the following example:
console.log("Example of converting int to float type"); // declare an integer var x= 55; var convertedValue = parseFloat(x).toFixed(2); console.log("Original Value=",x); console.log("Converted Value=",convertedValue);
Example of converting int to float type Original Value= 55 Converted Value= 55.00
Let’s have a quick review of the topics discussed in this article.
- Difference between int and float data type.
- Number data types of most of the programming languages.
- When do we need to convert int into float type?
- Methods used to convert int to float type.
- How to convert int to float type?
Thank you for reading! If you found this article beneficial, don’t forget to share and comment below👇.
Happy coding. 👍