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;   
        } 

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

猜你喜欢

转载自www.cnblogs.com/niuben/p/12912253.html
今日推荐