Thursday, March 24, 2011

Outputs a human readable number

<?php
    
#    Output easy-to-read numbers
    #    by james at bandit.co.nz
    
function bd_nice_number($n) {
        
// first strip any formatting;
        
$n = (0+str_replace(",","",$n));
       
        
// is this a number?
        
if(!is_numeric($n)) return false;
       
        
// now filter it;
        
if($n>1000000000000) return round(($n/1000000000000),1).' trillion';
        else if(
$n>1000000000) return round(($n/1000000000),1).' billion';
        else if(
$n>1000000) return round(($n/1000000),1).' million';
        else if(
$n>1000) return round(($n/1000),1).' thousand';
       
        return 
number_format($n);
    }
?>
Outputs:

247,704,360 -> 247.7 million
866,965,260,000 -> 867 billion

No comments:

Post a Comment