note 107259 added to function.bcscale

From: Date: Wed, 25 Jan 2012 14:21:01 +0000
Subject: note 107259 added to function.bcscale
Groups: php.notes 
Request: Send a blank email to php-notes+get-185740@lists.php.net to get a copy of this message
Previously stated one liner trailing 0 removing using rtrim() works fine except following class of subcases: <?php echo rtrim('100.0000', '0.'); // 1 instead 100 echo rtrim('1230.00000000', '0.'); // 123 instead 1230 ?> What seems to be working to me is using regular expression replacement. Althought it`s probably not that fast as rtrim(), I hope it should be more reliable: <?php function removeFloatNumStringZeroTrailer($input) { $patterns = array('/[\.][0]+$/','/([\.][0-9]*[1-9])([0]*)$/'); $replaces = array('','$1'); return preg_replace($patterns,$replaces,$input); } echo removeFloatNumStringZeroTrailer('100.0000'); // says 100 echo removeFloatNumStringZeroTrailer('1230.00000000'); // says 1230 ?> ---- Server IP: 69.147.83.197 Probable Submitter: 88.100.169.180 ---- Manual Page -- http://www.php.net/manual/en/function.bcscale.php Edit -- https://master.php.net/note/edit/107259 Del: integrated -- https://master.php.net/note/delete/107259/integrated Del: useless -- https://master.php.net/note/delete/107259/useless Del: bad code -- https://master.php.net/note/delete/107259/bad+code Del: spam -- https://master.php.net/note/delete/107259/spam Del: non-english -- https://master.php.net/note/delete/107259/non-english Del: in docs -- https://master.php.net/note/delete/107259/in+docs Del: other reasons-- https://master.php.net/note/delete/107259 Reject -- https://master.php.net/note/reject/107259 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#185740) next »