php去除金额后面多余的0(零)

第一种:

使用floatval()

 

第二种:

rtrim(rtrim($str, '0'), '.');

比如$str=2.360000; 最后会输出2.36

 

第三种使用正则:

        /**  
         * 去除多余的0  
         */   
        function del0($s)   
        {   
            $s = trim(strval($s));   
            if (preg_match('#^-?\d+?\.0+$#', $s)) {   
                return preg_replace('#^(-?\d+?)\.0+$#','$1',$s);   
            }    
            if (preg_match('#^-?\d+?\.[0-9]+?0+$#', $s)) {   
                return preg_replace('#^(-?\d+\.[0-9]+?)0+$#','$1',$s);   
            }   
            return $s;   
        } 

效果都是一样,但是第一种最简单

 

posted @ 2020-05-18 18:22  牛奔  阅读(2562)  评论(0编辑  收藏  举报