PHP/Data Type/Float

Материал из Web эксперт
Перейти к: навигация, поиск

Add a PHP block and create a variable that holds a floating-point number

   <source lang="html4strict">

<?

  1. Create a variable that holds a floating-point number:

$floatVar = "1542.2232235";

echo "

float: $floatVar

";

?>

      </source>
   
  


Addition of two floating-point variables compared to a variable can lead to an unexpected result.

   <source lang="html4strict">

<?php

   $a=50.3; 
   $b=50.4; 
   $c=100.7; 
   
   if ($a + $b == $c) { 
       echo "$a + $b == $c\n"; 
   } 
   else { 
       echo "$a + $b != $c\n"; 
   }

?>

 </source>
   
  


Addition of two floating-point variables compared to a variable with the expected value result can lead to an unexpected result.

   <source lang="html4strict">

<?php $a=50.3; $b=50.4; $c=100.7; if ($a + $b == $c) {

 echo "$a + $b == $c\n";

} else {

 echo "$a + $b != $c\n";

} ?>

 </source>
   
  


Calculate the area of a circle of given radius

   <source lang="html4strict">

<?php $radius = 2.0; $pi = 3.14159; $area = $pi * $radius * $radius; echo("radius = "); echo($radius); echo("
area = "); echo($area); ?>

      </source>
   
  


Compare floating-point values,avoid the == operator and use the <, >, >=, and

   <source lang="html4strict">

<?php for ($i = 0; $i < 100; $i += 0.1) {

   if ($i == 50) echo "$i == 50" . "\n"; 
   if ($i >= 50 && $i < 50.1) echo "$i >= 50 && $i < 50.1" . "\n"; 

} ?>

 </source>
   
  


Comparing floating point numbers

   <source lang="html4strict">

<? $price_1 = 0.00001; $price_2 = 0.000001;

if(abs($price_1 - $price_2) < 0.00001) {

   print "$price_1 and $price_2 are equal.";

} else {

   print "$price_1 and $price_2 are not equal.";

} ?>

 </source>
   
  


Comparing float values

   <source lang="html4strict">

<? $cost = 1.22; $limit = 1.00; function check_limit($total_cost, $credit_limit) {

    if ($total_cost > $credit_limit) :
         return 0;
    endif;
    return 1;

} if (check_limit($cost, $limit)):

   print "OK";

else :

   print "Balance less than $".$limit."!";

endif; ?>

 </source>
   
  


Compound Interest

   <source lang="html4strict">

<?php function calc_compound_interest($a, $r, $f, $y) {

   return $a * pow((1 + ($r / $f)), ($f * $y));

} echo calc_compound_interest(5000, .0325, 365, 3), "
"; ?>

 </source>
   
  


Continuously Compounded Interest Calculator

   <source lang="html4strict">

<?php function calc_continuous_compound_interest($a, $r, $y) {

   return $a * pow(M_E, $r * $y);

} echo calc_continuous_compound_interest(5000, .0325, 3), "
"; ?>

 </source>
   
  


Define Integer, double and string value and output them

   <source lang="html4strict">

<?php $integer_value = 1; $double_value = 1.2345678e6; $string_value = "This is a string"; echo("
integer value: "); echo $integer_value; echo("
double value: "); echo($double_value); echo("
string value: "); echo($string_value); ?>


      </source>
   
  


Finding the distance between two points

   <source lang="html4strict">

<?php function sphere_distance($lat1, $lon1, $lat2, $lon2, $radius = 6378.135) {

   $rad = doubleval(M_PI/180.0);
   
   $lat1 = doubleval($lat1) * $rad;
   $lon1 = doubleval($lon1) * $rad; 
   $lat2 = doubleval($lat2) * $rad;
   $lon2 = doubleval($lon2) * $rad;
   
   $theta = $lon2 - $lon1;
   $dist = acos(sin($lat1) * sin($lat2) + cos($lat1) * cos($lat2) * cos($theta));
   if ($dist < 0) { $dist += M_PI; }
   return $dist = $dist * $radius;

} $lat1 = 40.; $lon1 = -50.; $lat2 = 37; $lon2 = -122; $dist = sphere_distance($lat1, $lon1, $lat2, $lon2); printf("%.2f\n", $dist * 0.621); ?>

 </source>
   
  


Floating-point arithmetic:

   <source lang="html4strict">

$a = 1.132324;

   $b = $a + 1;
   $b = $a + 1.0;
   $c = 1.1e15;
   $d = (0.1+0.7) * 10;
 
 </source>
   
  


Floating point numbers ("doubles") can be specified using any of the following syntaxes:

   <source lang="html4strict">

<? $a = 1.234; $a = 1.2e3; ?>

 </source>
   
  


Float is converted to an integer before the binary and (&) operation is executed.

   <source lang="html4strict">

<?php $a = 3.5; $b = $a & 2; echo "$a & 2 = $b"; ?>

 </source>
   
  


PHP feet-to-meters converter

   <source lang="html4strict">

<head>

   <title>Feet to meters conversion</title>

</head> <body> <?php $feet = htmlentities($_GET["feet"]); if ($_GET[feet] != NULL){

   echo "$feet feet converts to ";
   echo $feet * 0.3048;
   echo " meters.
";

} ?> <form action="<?php echo(htmlentities($_SERVER["PHP_SELF"])); ?>" method="GET">

   <label>Feet:
       <input type="text" name="feet" value="<?php echo $feet; ?>" />
   </label>
   <input type="submit" value="Convert!" />

</form> </body> </html>

 </source>
   
  


Simple Interest

   <source lang="html4strict">

<?php function calc_simple_interest($principal, $annualrate, $years) {

   return $principal * $annualrate * $years;

} echo calc_simple_interest(22000, .0539, 6), "
"; echo calc_simple_interest(1000, .085, .5); ?>

 </source>
   
  


Specifying Precision

   <source lang="html4strict">

<?

   printf( "%.2f", 5.333333);

?>

 </source>
   
  


Storing Floating Point Numbers in PHP

   <source lang="html4strict">

<?php

   /* Standard Floating Point Notation */
   $my_float = 5.1;
   /* Scientific Floating Point Notation of same number */
   $my_float = .051e2;    

?>

 </source>
   
  


the float is converted to an integer before the binary and (&) operation is executed.

   <source lang="html4strict">

<?php $a = 3.5; $b = $a & 2; echo "$a & 2 = $b"; ?>

 </source>