ASPN ActiveState Programmer Network  
ActiveState, a division of Sophos
/ Home / Perl / PHP / Python / Tcl / XSLT /
/ Safari / My ASPN /
Cookbooks | Documentation | Mailing Lists | Modules | News Feeds | Products | User Groups
Submit Recipe
My Recipes

All Recipes
All Cookbooks


View by Category

Title: PHP Commify Function
Submitter: Alvin Estevez (other recipes)
Last Updated: 2006/07/07
Version no: 1.4
Category: Text Processing and Rx

 

4 stars 3 vote(s)


Description:

This function inserts a comma separator into every 3 digits. For example, 100000 will be 100,000.

Syntax:
commify(Number);

For Example:
commify (100000);
Output will be: $100,000

Source: Text Source

<?php

## Passing values to the commify() function
$number  = 100000;
$display_number = commify ($number);

## Display output
print "Input: $number" . "<br>"; 
print "Output: $display_number" . "<br>"; 

## The Commify Function
function commify ($str) { 
        $n = strlen($str); 
        if ($n <= 3) { 
                $return=$str;
        } 
        else { 
                $pre=substr($str,0,$n-3); 
                $post=substr($str,$n-3,3); 
                $pre=commify($pre); 
                $return="$pre,$post"; 
        }
        return($return); 
}

?>

Discussion:

This function can be used for financial, real estate, or any online reporting system where you want to make numbers more legible to the user.

Check out the dollarfy() function: http://aspn.activestate.com/ASPN/Cookbook/PHP/Recipe/202053

Courtesy of Alvin Estevez, Enigma Software Group, Inc.
http://www.enigmasoftwaregroup.com

Visit my anti-spyware blog ( http://www.anti-spyware-101.com ) for the latest database of spyware manual removal instructions and other Spyware resources, which consists of complete component profiles (files, registry settings, MD5 file signatures, and other diagnostic information) of various adware applications, spyware programs, backdoor trojans, browser hijackers, tracking cookies, worms, keyloggers, etc. that commonly afflict PCs connected to the Internet.



Add comment

Number of comments: 1

Don't reinvent the wheel, Jay Pipes, 2003/10/24
Read PHP manual. Use the number_format() function.
Add comment



Highest rated recipes:

1. Single Linked List

2. main - python-like if ...

3. iPHP: Semi-interactive ...

4. tinySendMail

5. Microsoft Access ...

6. DB_eSession PHP class ...

7. PHP MySQL Search Class

8. Pass Javascript arrays ...

9. Microsoft Access ...

10. Password generation




Privacy Policy | Email Opt-out | Feedback | Syndication
© 2006 ActiveState Software Inc. All rights reserved.