How to Convert Integer Number to Float Using PHP

In this tutorial, learn methods to convert integer number to float in PHP. The short answer is to use the PHP sprintf() for converting integer type value to float. You can also use other functions of PHP whose methods are given below to perform this task.

The integer value contains only numbers and no decimal points. However, float contains the point(.) after number with some places of decimal like one(9.0), two(3.00), three(13.000), etc. In the PHP variable type, the float variable is also called a double variable as both are the same in PHP.

So, let’s start learning each method with the examples given below.

Convert Integer Number to Float Using sprintf() in PHP

If you want to convert Integer Number to Float in PHP. You have to use the sprintf function which takes two arguments. The sprintf() takes the first argument which decides the decimal point after the numbers. The second argument is the integer variable which is the numeric value.

Output

109.00

The above output shows the float value with two decimal places. You have to increase the first argument value to more numbers to get more digits after the decimal point.

Change Integer Value to Float With number_format() in PHP

In addition to the above method to convert integer numbers to float in PHP. You can also use PHP number_format() to get the conversion. The number_format function takes two arguments with the first argument as the integer variable which you have to convert. The second argument is the number of digits after the decimal point to include.

Output

4439.00

The above example showing the two decimal places added after the conversion. However, you can add more decimal places by increasing the second argument value of the function.

What You Will Get When You Find the Type of Float Variable Using PHP

Now, you know two different methods of converting the integer type value to float in PHP. If you want to find the type of float variable of the above-converted numbers, what you will get in the output.

As you can find the details of the PHP variable that there is no difference in double and float. Both are the same thing and can be used in PHP to define the float variable. However, if you want to find the type of float variable, you will get only double and not float.

Let’s check it out in the example given below with the float variable.

Output

double

The above output showing as ‘double’ which describes the float variable in PHP. The gettype() is the function of PHP to find the type of any variable.

You may also like to read

I hope you like this post on how to convert an integer value to float using PHP.