note 91848 deleted from function.trim by danbrown
| From: | danbrown@php.net | Date: | Mon, 29 Jun 2009 14:33:23 +0000 |
| Subject: | note 91848 deleted from function.trim by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-157669@lists.php.net to get a copy of this message | ||
Note Submitter: Riven
----
As an improvement to the zero trimming notes from 'rifter at cox dot net' and 'chris
dot cowart at gmail dot com' four years ago, I tweaked their code so that it will avoid
trimming integers:
<?php
function strip_trailing_zeros($num) {
if( $num && is_float($num+1) )
return rtrim(rtrim($num, 0), '.');
else
return $num;
}
?>
Note that my version only removes trailing zeros, as that is what I required. e.g. 0.034500 becomes
0.0345. To remove leading zeros as well, you only need change the inner most 'rtrim' to
'trim'.