note 23502 added to function.round
| From: | charly at pache dot ch | Date: | Fri, 19 Jul 2002 17:33:40 +0000 |
| Subject: | note 23502 added to function.round | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-33317@lists.php.net to get a copy of this message | ||
For those who live in countries where the smallest piece of money is 0.05, this function may be
helpful to round prices after having added the VAT :
function arrondi($nombre)
{
// on arrondi au centième près : 7.9649 devient 7.66
$nombre=floor(($nombre * 100)+.5)/100;
// on sépare la partie entière de la partie flottante :
$tab = explode('.',$nombre);
// $tab[0] contient 7
// $tab[1] contient 96
$dixieme = substr($tab[1],0,1);// contient 9
$centieme = substr($tab[1],1,1);// contient 6
// on fait le test sur le centieme :
if($centieme<=7)
{
if($centieme<=2)
{
$retenue = 0;
$centieme = 0;
}
else // on est entre 3 et 7 compris
{
$retenue = 0;
$centieme = 5;
}
}
else // si on est à 8 ou 9 :
{
$retenue = 1;
$centieme = 0;
}
if($retenue=="1") $dixieme = "0";
// on reconstruit le nombre sans la retenue :
$nombre = "".$tab[0].".".$dixieme."".$centieme;
// on ajoute la retenue :
$nombre += $retenue;
return $nombre;
}
Charly & Sabine
--
http://www.php.net/manual/en/function.round.php
http://master.php.net/manage/user-notes.php?action=edit+23502
http://master.php.net/manage/user-notes.php?action=delete+23502
http://master.php.net/manage/user-notes.php?action=reject+23502