PHP Float

In this chapter you will learn:

  1. What is float number
  2. PHP Float Literal
  3. Example - floating-point arithmetic
  4. Example - Calculate the area of a circle of given radius

Description

Floats hold fractional numbers as well as very large integer numbers. Floating-point numbers are called real numbers or just floats.

Float Literal

We may also specify an exponent with your float, i.e., 3.14159e4 is equal to 31415.9.


<?PHP// j av  a  2 s .  c o  m
$a = 1.234; 
print ($a);
print ("\n");
$a = 1.2e3;
print ($a);
?>

The code above generates the following result.

Example

Here are some examples of floating-point arithmetic:


<?PHP/*from   j  av  a2 s . c  o  m*/
$a = 1.132324; 
print($a);
$b = $a + 1.9; 
print($b);
$b = $a + 1.0; 
print($b);
$c = 1.1e15; 
print($c);
$d = (0.1+0.7) * 10; 
print($d);
?>

The code above generates the following result.

Example 2

Calculate the area of a circle of given radius


<?php//from   jav  a  2s .com

$radius = 2.0;
$pi = 3.14159;
$area = $pi * $radius * $radius;

echo("radius = ");
echo($radius);
echo("<BR>area = ");
echo($area);
?>

The code above generates the following result.

Next chapter...

What you will learn in the next chapter:

  1. What is data type cast
  2. Syntax to do data type cast
  3. Example - PHP data type casting
  4. PHP cast you can do
Home » PHP Tutorial » PHP Data Types
PHP Data Types
PHP Boolean
PHP Integer
PHP Float
PHP Data Type Cast
PHP Automatic Type Conversion
PHP Super globals
PHP References
PHP Constants