PHP Doku:: Abrunden - function.floor.html

Verlauf / Chronik / History: (1) anzeigen

Sie sind hier:
Doku-StartseitePHP-HandbuchFunktionsreferenzMathematische ErweiterungenMathematische FunktionenMathematische Funktionenfloor

Ein Service von Reinhard Neidl - Webprogrammierung.

Mathematische Funktionen




(PHP 4, PHP 5)



float floor ( float $value )

Liefert die nächste ganze Zahl, die kleiner oder gleich dem Parameter value ist.



Der abzurundende Wert


Liefert den abgerundeten Wert zurück. Dieser ist nach wie vor vom Typ float und nicht vom Typ integer wegen des größeren Wertebereichs von float.


Beispiel #1 floor() Beispiel

echo floor(4.3);   // 4
echo floor(9.999); // 9
echo floor(-3.14); // -4

Siehe auch

26 BenutzerBeiträge:
- Beiträge aktualisieren...
jochem_php dot net at rechtbank dot org
28.05.2010 12:26
Another quick way to round down to the nearest 100 (including 0):

function floor100($num) {
    return (
floor($num / 100) * 100);
seppili_ at gmx dot de
5.03.2010 17:52
I use this function to floor with decimals:

function floordec($zahl,$decimals=2){   
5.02.2010 14:07
Here's a shorter versiion of "round to nearest hundred"


function rd100($num)
round($num / 100) * 100;

lewis at lewishenshall dot co dot uk
26.02.2009 8:12
If you're wanting to round down to the nearest hundred:

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


fragov at gmail dot com
15.01.2009 6:39
Have solved a "price problem":

= floor($row->price*1000) - floor($row->price)*1000)/10;
mike dot devise at gmail dot com
18.10.2008 2:02
marc at moblaster's rounding technique for handling financial truncation fails for 0.0, and probably has unintended consequences for values less than 0.  Something like this should do the trick:

function truncateMoneyFloat($amount)
    if ($amount > 0.005) return round($amount - 0.005,2);
    elseif ($amount < -0.005) return round($amount + 0.005,2);
    else return 0.0;

However, +1 billion for jolyon at mways' comment regarding abandoning floats entirely when processing financial data.  Create a money class and track the value internally with an integer.  Provide methods to scale by float values where the rounding behavior can be controlled by an explicit policy variable.
benrr101 at gmail dot com
11.06.2008 18:39
But, if you want the number closest to zero, you could use this:
if($foo > 0) {
  } else {

greene dot mc at removethispart dot gmail dot com
27.05.2008 22:08
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:
01-Dec-2007 04:22
echo floor(1.6);  // will output "1"
echo floor(-1.6); // will output "-2"

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

echo intval(1.6);  // will output "1"
echo intval(-1.6); // will output "-1"
jay at w3prodigy dot com
25.03.2008 21:01

$int = 0.99999999999999999;
echo floor($int); // returns 1


$int = 0.9999999999999999;
echo floor($int); // returns 0
marc at moblaster dot com
1.03.2008 0:37
There is actually a much easier and simpler way to chop off any fraction of a penny in financial calculations. Here is the code:

function round_down_to_penny( $p ) {
    return $p;

In other words, just subtract 0.005 and round that to 2 decimal places.

You can easily verify that any price from say 7.21 to 7.2199999 ends up as 7.21.
1.12.2007 10:22
echo floor(1.6);  // will output "1"
echo floor(-1.6); // will output "-2"

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

echo intval(1.6);  // will output "1"
echo intval(-1.6); // will output "-1"
moveAtCharacter at omnibus dot omnibus dot edu dot pl
23.10.2007 14:36
How about something like this:
function goodFloor($x) {
 return floor(round($x, strlen((string)($x))));

It's not elegant but at least short...

Jared Fine
21.06.2007 16:43
Good catch Matthäus.

After thinking about it a bit more, it seems there isn't an elegant solution. The only way to properly handle it is to do some string manipulation, chop it to a specific decimal length, potentially string pad it, then do your rounding and floor.
Matthäus Brandl
18.05.2007 0:04
As a response to Jared Fine:
There is a little problem with your suggestion.

floor(2.95) == 2 != floor(round(2.95, 1)) == 3

as round(2.95, 1) == 3.0
Jared Fine
30.05.2006 5:16
Due to the floating point precision issue ( if you round() one additional decimal point prior to calling floor() you will get the expected result.

For example:
echo floor((0.1 + 0.7) * 10); // echo's 7
echo floor(round((0.1 + 0.7) * 10, 1)); // echo's 8
Andreas Blixt
27.05.2006 22:26
Simpler version of Bruteork's example (though not making use of floor()):
if ($counter % 2 == 0) echo "even";
peter at harbour-design dot co dot uk
25.01.2005 10:52
If you are after just removing the decimal places and returning an integer, try:

= 5.67;
$iNegDecimalNo = -5.67;

print (int)
print (int)

result will:


No rounding up or down, just truncation and works for positive and negative numbers.
sam at bigroomstudios dot com
31.08.2004 17:59
Here's a way to get around jolyon's 79.99 problem (below).  If you want to take a float value representing a price and drop off any fractions of a cent, use this:


function round_to_penny($amount){
$string = (string)($amount * 100);

$string_array = split("\.", $string);
$int = (int)$string_array[0];
$return = $int / 100;


round_to_penny(79.99999) ); //result: 79.99


Probably not super efficient, but get's the job done if you've already invested in using float values.

jolyon at mways dot co dot uk
10.08.2004 18:41
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:

= 79.99;

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

$price = $price * 100;

$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 :)
illyena at musefish dot net
13.05.2004 0:53
For calculating the number of days, hours, minutes and seconds to an event.

= date(mktime(8,0,0,6,25,2004)); //remember that mktime is hour,min,sec,month,day,year
$now = date("U"); // "U" is the number of seconds since the epoch, equivilant to using "YmdHis"

$time = $then - $now; //gets the number of seconds between now and the event
$days = floor($time/86400); //rounds down to the whole number, in this case # of days
echo $days." Days";
$time = $time - ($days*86400); //leaves you with the amount of time ramaining after subtracting the days
$hours = floor($time/3600); //rounds down to the whole number, in this case # of hours
echo $hours." Hours";
$time = $time - ($hours*3600); //leaves you with the amount of time ramaining after subtracting the hours
$min = floor($time/60); //rounds down to the whole number, in this case # of minutes
echo $min." Minutes";
$sec = $time - ($min*60); //leaves you with the amount of time ramaining after subtracting the minutes which is equivilant to the remainins seconds
echo $sec." Seconds";
twindagger2k3 at NOSPAMyahoo dot com
14.02.2004 0:58
In response to PHP Helper, the floor function does strip the decimal part if the number is positive. However, if the number is negative, it will not. for example:


= 5.6;
floor($test); //5

$test = -5.6;
floor($test); //-6


The rounding mentioned in PHP Helper's post will work for both positive and negative numbers.
PHP Helper
20.12.2003 11:39
floor basically truncates, or chops off everything to the right of a decimal. For instance, if you have a length of 5.1234, but just wanted the whole number, you could use the following code:

= 5.1234; //this is our original length
$length = floor($length); //length is truncated, original variable name is kept
print "$length"; //this prints our result

This code would print the following: 5

Now, although there is a specific function in PHP for rounding, rounding can also be performed with the floor function. Let's say we wanted to round the length to the hundredths place.

= 5.1234;
$length = floor(($length) * 100 + .5) * .01;

The result is: 5.12

This works because, first, the length is multiplied by 100, which moves the decimal point to the right two places, giving us the value of 512.34. Next .5 is added to the length, which gives us a value of 512.84. Then the floor function truncates the value, leaving us with 512. Lastly, to compensate for multiplying by 100 earlier, now we must divide by 100, or in this case, multiply by .01. This moves the decimal point back 2 places to it's original place and gives us the rounded value of 5.12.

We can also round to other values, such as the thousandths, by adjusting the code as follows:

= 5.1234;
$length = floor(($length) * 1000 + .5) * .001;

Result: 5.123
asp55 at digiclub dot org
14.02.2003 23:52
Just a quick example of how to use this method

The first is just used to determine whether a number is even or odd:

if(($x - (2 * floor($x/2))) == 0) echo "even";
else echo

The second is just to determine a persons age by comparing their birthday with the current date and rounding down:

<? $age = floor((date(Ymd) - $bday)/10000); ?>

10.09.2002 12:08
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:

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!):

$f = 1 + @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(), ...
thouartjay at hotmail dot com
24.06.2002 3:37
In response to netben's comment, another conversion for a string is:

$myStr = "29.01";
$myStr = (float) $myStr;

In all, any string (as used as in this example) will be treated as float, and no conversion necessary.
alhall at jcw dot org
27.02.2002 18:17
//Using floor() to round decimals.
  $discount=floor(($discount * 100)+.5)/100;
//$discount after floor() = 5.09

PHP Powered Diese Seite bei
The PHP manual text and comments are covered by the Creative Commons Attribution 3.0 License © the PHP Documentation Group - Impressum - mail("TO:Reinhard Neidl",...)