note 52949 deleted from function.is-int by nlopess

From: Date: Mon, 18 Dec 2006 11:12:48 +0000
Subject: note 52949 deleted from function.is-int by nlopess
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-120727@lists.php.net to get a copy of this message
Note Submitter: ---- 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

« previous php.notes (#120727) next »