floor

(PHP 4, PHP 5, PHP 7)

floorОкругляет дробь в меньшую сторону

Описание

float floor ( float $value )

Возвращает ближайшее целое число, округляя value в меньшую сторону.

Список параметров

value

Числовое значение для округления

Возвращаемые значения

Значение value, округленное в меньшую сторону до ближайшего целого числа. Возвращаемое значение функции floor() по прежнему типа float потому что диапазон значений float обычно больше, чем у integer.

Примеры

Пример #1 Пример использования floor()

<?php
echo floor(4.3);   // 4
echo floor(9.999); // 9
echo floor(-3.14); // -4
?>

Смотрите также

  • ceil() - Округляет дробь в большую сторону
  • round() - Округляет число типа float

Коментарии

Автор:
mathematical functions lack a floating point version of the modulo operation, which returns the difference between the floor() of the argument and the argument itself:

<?php
function fmod($value) {
  return 
$value floor($value);
}
?>

Very useful with trigonometric functions to reduce the angle argument to a circle that includes angle 0.

Useful also to reduce an arbitrarily large floating point value into an entropy source, by first transforming this value into a pair using logarithm functions with distinct bases (add 1 if the function can return 0, to avoid floating point errors with logarithms!):

<?php
$f 
+ @disk_free_space("/tmp");
$r = (int)(fmod(Log($f)) * 0x7FFFFFFF)
^ (int)(
fmod(Log10($f)) * 0x7FFFFFFF)
?>

Then $r can be used as a good entropy source, if the free space in your temporary folder used by PHP is constantly evolving within a large range of values.

You can combine this value by xoring it with other values such as time(), (int)microtime(), ip2long($_SERVER['REMOTE_ADDR'], $_SERVER['REMOTE_PORT'], getmypid(), ...
2002-09-10 06:08:35
http://php5.kiev.ua/manual/ru/function.floor.html
Beware of FLOAT weirdness!

Floats have a mind of their own, and what may look like an integer stored in a float isn't.

Here's a baffling example of how floor can be tripped up by this:

<?php
$price 
79.99;

print 
$price."\r\n";     // correct result, 79.99 shown

$price $price 100;

print 
$price."\r\n";    // correct result, 7999 shown

print floor($price);    // 7998 shown! what's going on?
?>

The thing to remember here is that the way a float stores a value makes it very easy for these kind of things to happen. When the 79.99 was multiplied by 100, the actual value stored in the float was probably something like 7998.9999999999999999999999999999999999, PHP would print out 7999 when the value is displayed but floor would therefore round this down to 7998.

THe moral of this story - never use float for anything that needs to be accurate! If you're doing prices for products or a shopping cart, then always use an integer and store prices as a number of pence, you'll thank me for this later :)
2004-08-10 12:41:48
http://php5.kiev.ua/manual/ru/function.floor.html
Автор:
<?php
 
echo floor(1.6);  // will output "1"
 
echo floor(-1.6); // will output "-2"
?>

instead use intval (seems to work v5.1.6):

<?php
 
echo intval(1.6);  // will output "1"
 
echo intval(-1.6); // will output "-1"
?>
2007-12-01 03:22:27
http://php5.kiev.ua/manual/ru/function.floor.html
Note:

<?php
$int 
0.99999999999999999;
echo 
floor($int); // returns 1
?>

and

<?php
$int 
0.9999999999999999;
echo 
floor($int); // returns 0
?>
2008-03-25 14:01:12
http://php5.kiev.ua/manual/ru/function.floor.html
I believe this behavior of the floor function was intended.  Note that it says "the next lowest integer".  -1 is "higher" than -1.6.  As in, -1 is logically greater than -1.6.  To go lower the floor function would go to -2 which is logically less than -1.6.

Floor isn't trying to give you the number closest to zero, it's giving you the lowest bounding integer of a float.

In reply to Glen who commented:
 Glen
01-Dec-2007 04:22
<?php
 
echo floor(1.6);  // will output "1"
 
echo floor(-1.6); // will output "-2"
?>

instead use intval (seems to work v5.1.6):

<?php
 
echo intval(1.6);  // will output "1"
 
echo intval(-1.6); // will output "-1"
?>
2008-05-27 16:08:40
http://php5.kiev.ua/manual/ru/function.floor.html
But, if you want the number closest to zero, you could use this:
<?php
 
if($foo 0) {
   
floor($foo);
  } else {
   
ceil($foo);
  }
?>

-benrr101
2008-06-11 12:39:08
http://php5.kiev.ua/manual/ru/function.floor.html
Have solved a "price problem":

<?php
$peny 
floor($row->price*1000) - floor($row->price)*1000)/10;
?>
2009-01-14 23:39:48
http://php5.kiev.ua/manual/ru/function.floor.html
If you're wanting to round down to the nearest hundred:

<?php
function rounddowntohundred($theNumber) {
    if (
strlen($theNumber)<3) {
   
$theNumber=$theNumber;
    } else {
   
$theNumber=substr($theNumber0strlen($theNumber)-2) . "00";

    }
    return 
$theNumber;

}
?>
2009-02-26 01:12:21
http://php5.kiev.ua/manual/ru/function.floor.html
I use this function to floor with decimals:
<?php

function floordec($zahl,$decimals=2){   
     return 
floor($zahl*pow(10,$decimals))/pow(10,$decimals);
}
?>
2010-03-05 10:52:16
http://php5.kiev.ua/manual/ru/function.floor.html
Автор:
A correction to the funcion floor_dec from the user "php is the best".
If the number is 0.05999 it returns 0.59 because the zero at left position is deleted.
I just added a '1' and after the floor or ceil call remove with a substr.
Hope it helps.

function floor_dec($number,$precision = 2,$separator = '.') {
  $numberpart=explode($separator,$number);
  $numberpart[1]=substr_replace($numberpart[1],$separator,$precision,0);
  if($numberpart[0]>=0) {
    $numberpart[1]=substr(floor('1'.$numberpart[1]),1);
  } else {
    $numberpart[1]=substr(ceil('1'.$numberpart[1]),1);
  }
  $ceil_number= array($numberpart[0],$numberpart[1]);
  return implode($separator,$ceil_number);
}
2014-01-23 10:45:41
http://php5.kiev.ua/manual/ru/function.floor.html
Автор:
Warning: do not use Seppili's function to floor with decimals. It will lead you to a lot of trouble because of float number precision.

For example floordec(0.29, 2) will result in 0.28.

Here's a little fix that will help you get around some problems:

<?php
function floordec($value,$decimals=2){   
    return 
floor($value*pow(10,$decimals)+0.5)/pow(10,$decimals);
}
?>
2015-12-27 23:59:32
http://php5.kiev.ua/manual/ru/function.floor.html
Автор:
<?php
echo (2.3 100) . ' - ' round(2.3 1000) .  ' - ' floor(2.3 100);
?>.

Result:
230 - 230 - 229

Be careful!
2019-10-24 15:14:03
http://php5.kiev.ua/manual/ru/function.floor.html

    Поддержать сайт на родительском проекте КГБ