note 52949 added to function.is-int
| From: | php-general at lists dot php dot net | Date: | Wed, 18 May 2005 18:46:26 +0000 |
| Subject: | note 52949 added to function.is-int | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-89594@lists.php.net to get a copy of this message | ||
function precisionNumero($numero, $precision, $noDevuelveNulo) {
if ( (isset($numero)) && (is_numeric($numero)) ) { //Es un número
if ( ((int)$numero) == ((float)$numero) ) { //Entero
$entero=(int)$numero;
}
else { //Real
if ((substr($numero,0,1)) ==".") {//Real que comienza por .
$entero="0";
$patronPrecision=".%".$precision."s";
sscanf ($numero,$patronPrecision, $decimal);
}
else { //real estandar
$patronPrecision="%d.%".$precision."s";
sscanf ($numero, $patronPrecision, $entero,$decimal);
}
}
for ($i=strlen($decimal); $i<$precision ; $i++) {
$decimal.="0";
}
}
else { // No es un numero valido
if ($noDevuelveNulo) {
$entero="0";
for ($i=0; $i<$precision ; $i++) {
$decimal.="0";
}
}
else return NULL;
}
return ($entero.".".$decimal);
}
precisionNumero("1.2.3", 2, true) returns 0.00
precisionNumero("1.23", 3, true) returns 1.230
precisionNumero(".23", 5, true) returns 0.23000
precisionNumero("1.", 7, true) returns 1.0000000
precisionNumero("1.2.3", 3, false) returns NULL
precisionNumero("0.0", 3, false) returns 0.000
----
Manual Page -- http://www.php.net/manual/en/function.is-int.php
Edit -- http://master.php.net/manage/user-notes.php?action=edit+52949
Delete: added to the manual -- http://master.php.net/manage/user-notes.php?action=delete+52949&report=yes&reason=added+to+the+manual
Delete: bad code -- http://master.php.net/manage/user-notes.php?action=delete+52949&report=yes&reason=bad+code
Delete: spam -- http://master.php.net/manage/user-notes.php?action=delete+52949&report=yes&reason=spam
Delete: useless -- http://master.php.net/manage/user-notes.php?action=delete+52949&report=yes&reason=useless
Delete: other reasons -- http://master.php.net/manage/user-notes.php?action=delete+52949&report=yes
Reject -- http://master.php.net/manage/user-notes.php?action=reject+52949&report=yes
Search -- http://master.php.net/manage/user-notes.php