note 102473 deleted from function.round by danbrown
| From: | danbrown@php.net | Date: | Wed, 16 Feb 2011 18:12:05 +0000 |
| Subject: | note 102473 deleted from function.round by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-177049@lists.php.net to get a copy of this message | ||
Note Submitter: pato
----
Función que devuelve un entero incrementado en 1 unidad si el redondeo de decimales es mayor o
igual que 5. Mostrando el resultado con presición 0, osea sin decimales.
Ejemplo:
$lnNro = "6,4444445";
$arr = split(",",$lnNro);
$decimales = $arr[1];
$car = redondeo($decimales);
if ($car >=5)
$lnNro = $arr[0] + 1;
else
$lnNro = $arr[0];
echo $lnNro; //output: 7
//$lnNro = "6,4443445"; output: 6
function redondeo($decimales)
{
$data=array();
while ($decimales !=''){
$car = substr($decimales,0,1);
$data[]=$car;
$decimales = substr($decimales,1,strlen($decimales));
}
$i=count($data);
while ($i>=0){
if ($i > 0){
if ($data[$i] >= 5){
$data[$i-1] = $data[$i-1] +1;
}
}
$i--;
}
return $data[0];
}