In this part, we will discuss the PHP floats. In the previous part, we have already learned about numbers and if you don’t check this tutorial then you must study it thoroughly to understand this part of series.
Below is the link to the previous part.
Let’s start, float contains the decimal numbers in points. For example, 4.34 is a float number. The number after . are also called significant figures. i.e. 3 and 4 are significant figures in 4.34.
As we were using the numbers within a program in previous part of our series. Similarly, we can use float.
This simple program will let you understanding the float.
$float = 1.5;
echo $float + 3.4;
The output of the program will be 4.9.
Here we are storing the value of float into a float variable. After adding 3.4 to this float variable we can see the output in the browser. Thus we use float within our program.
Just try a simple program given below in your text editor now.
//What is the output of this code??
Obviously the browser will show an error. The reason behind this error is that in PHP we can’t devide a number by zero.
We can use the different built-in function in PHP to perform different mathematical functionalities by using float.
The list of these function is given below.
This function can limit the significant figures of a number to specific numbers. For example, when we divide the 4 by 3, we get the 1.3333333 answer. We can control the numbers of significant figures to specific numbers i.e. 1.3.
It takes two arguments, the first argument will be the variable that is storing the float data. And the second one will be the number of significant figures.
ceil will return the whole number coming next to floating number. For example, in the case of 1.333, the ceiling function will return 2 as after 1.3 the number without a significant figure is 2.
The floor function will round down the float number. For example, when we apply the function floor on the floating point number 1.333 the output will be 1.
This function tells us the data type of the variable. i.e. we can check whether the value stored in the variable is an integer or not.
After storing the value in variable i.e. $float, we will simply apply is_int($float) function, the program will return boolean. If the data store in the variable is an integer the output of the program will be 1. Else the program will return 0.
It will also perform a similar function as is_int. The only difference is this function will tell us that the weather the data stored in the variable is float or not.
The function is use to check the numeric values, i.e. it will check either a specific value is numeric or not. As a result, it will display the values that are numeric.
If you guys want to learn more about floats, then click here.