|
użytkowników online: 56
|
OPINIE UŻYTKOWNIKÓW
|
Gratulacje i dzięki! Trafiłem tu przypadkiem poszukując informacji na temat php+mysql. Wiele polskich stron powiela identyczne przykłady, klonuje te same kursy i lekcje... ten serwis okazał sie inny. Zasada "problem - rozwiazanie - wyjaśnienie" zdaje egzamin - zapewnia jasną, jednoznaczną i pewną pomoc w konkretnym przypadku. Porady są warte swojej ceny, przede wszystkim ze względu na przyjazną (także dla początkujących) formę i treść oraz bogate i stale powiększane zasoby. Polecam i pozdrawiam!
Kamil Dmowski
Polski Czerwony Krzyż
|
|
PODRĘCZNIK PHP 5.x, 4.x, 3.x - częściowo spolszczony / źródło: www.php.net
[Spis]
[A]
[B]
[C]
[D]
[E]
[F]
[G]
[H]
[I]
[J]
[K]
[L]
[M]
[N]
[O]
[P]
[Q]
[R]
[S]
[T]
[U]
[V]
[X]
[W]
[Z]
floor (PHP 3, PHP 4, PHP 5) floor -- Zaokrągla ułamki w dół Opisfloat floor ( float liczba )
Zwraca najbliższą liczbę całkowitą, mniejszą lub równą podanemu arumentowi
liczba.
Zwracana przez funkcję floor() wartość jest nadal typu
float ponieważ zakres tego typu jest zwykle większy niż zakres
typu integer.
Przykład 1. przykład zastosowania floor() |
echo floor(4.3); // 4
echo floor(9.999); // 9
|
|
Patrz także: ceil() i
round().
User Contributed Notespeter at harbour-design dot co dot uk
25-Jan-2005 10:52
If you are after just removing the decimal places and returning an integer, try:
<?
$iPosDecimalNo = 5.67;
$iNegDecimalNo = -5.67;
print (int)$iPosDecimalNo;
print (int)$iNegDecimalNo;
?>
result will:
5
-5
No rounding up or down, just truncation and works for positive and negative numbers.
paradoxheart at anime-sakura dot org dot uk
19-Nov-2004 01:50
Unfortunately, converting to a string does not completely eliminate the price problem.
If, for example, our calculation actually yielded the result 79.9899999999999 then naturally we'd want the floor function to return 79.98, but instead it will return 79.99 if you use the string conversion approach.
steven_p_yang at yahoo dot com
26-Oct-2004 11:24
Regarding jolyon's problem, a quick fix is this:
$price = "$price";
By doing this, you change $price to the string "7999". Now when you call floor, you get the expected result.
sam at bigroomstudios dot com
31-Aug-2004 05: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:
<?php
function round_to_penny($amount){
$string = (string)($amount * 100);
$string_array = split("\.", $string);
$int = (int)$string_array[0];
$return = $int / 100;
return $return;
}
print( round_to_penny(79.99999) ); ?>
Probably not super efficient, but get's the job done if you've already invested in using float values.
Cheers,
Sam
jolyon at mways dot co dot uk
10-Aug-2004 06: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:
<?
$price = 79.99;
print $price."\r\n"; $price = $price * 100;
print $price."\r\n"; print floor($price); ?>
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-May-2004 12:53
For calculating the number of days, hours, minutes and seconds to an event.
<?
$then = date(mktime(8,0,0,6,25,2004)); $now = date("U"); $time = $then - $now; $days = floor($time/86400); echo $days." Days";
$time = $time - ($days*86400); $hours = floor($time/3600); echo $hours." Hours";
$time = $time - ($hours*3600); $min = floor($time/60); echo $min." Minutes";
$sec = $time - ($min*60); echo $sec." Seconds";
?>
twindagger2k3 at NOSPAMyahoo dot com
14-Feb-2004 12: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:
<?php
$test = 5.6;
echo floor($test); $test = -5.6;
echo floor($test); ?>
The rounding mentioned in PHP Helper's post will work for both positive and negative numbers.
PHP Helper
20-Dec-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:
<?php
$length = 5.1234; $length = floor($length); print "$length"; ?>
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.
<?php
$length = 5.1234;
$length = floor(($length) * 100 + .5) * .01;
print "$length";
?>
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:
<?php
$length = 5.1234;
$length = floor(($length) * 1000 + .5) * .001;
print "$length";
?>
Result: 5.123
asp55 at digiclub dot org
14-Feb-2003 11: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 "odd";
?>
The second is just to determine a person
|