note 120232 added to function.round
| From: | greghenleatgmaildotcom | Date: | Mon, 28 Nov 2016 21:25:27 +0000 |
| Subject: | note 120232 added to function.round | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-207728@lists.php.net to get a copy of this message | ||
/**
* Round to first significant digit
* +N to +infinity
* -N to -infinity
*
*/
function round1stSignificant ( $N ) {
if ( $N === 0 ) {
return 0;
}
$x = floor ( log10 ( abs( $N ) ) );
return ( $N > 0 )
? ceil( $N * pow ( 10, $x * -1 ) ) * pow( 10, $x )
: floor( $N * pow ( 10, $x * -1 ) ) * pow( 10, $x );
}
echo round1stSignificant( 39144818 ) . PHP_EOL;
echo round1stSignificant( 124818 ) . PHP_EOL;
echo round1stSignificant( 0.07468 ) . PHP_EOL;
echo round1stSignificant( 0 ) . PHP_EOL;
echo round1stSignificant( -0.07468 ) . PHP_EOL;
/**
* Output
*
* 40000000
* 200000
* 0.08
* 0
* -0.08
*
*/
----
Server IP: 72.52.91.14
Probable Submitter: 148.129.129.172
----
Manual Page -- http://php.net/manual/en/function.round.php
Edit -- https://master.php.net/note/edit/120232
Del: integrated -- https://master.php.net/note/delete/120232/integrated
Del: useless -- https://master.php.net/note/delete/120232/useless
Del: bad code -- https://master.php.net/note/delete/120232/bad+code
Del: spam -- https://master.php.net/note/delete/120232/spam
Del: non-english -- https://master.php.net/note/delete/120232/non-english
Del: in docs -- https://master.php.net/note/delete/120232/in+docs
Del: other reasons-- https://master.php.net/note/delete/120232
Reject -- https://master.php.net/note/reject/120232
Search -- https://master.php.net/manage/user-notes.php