Welcome, guest | Sign In | My Account | Store | Cart

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

PHP, 26 lines
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
<?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); 
}

?>

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.

1 comment

Jay Pipes 18 years, 1 month ago  # | flag

Don't reinvent the wheel. Read PHP manual. Use the number_format() function.

Created by Alvin Estevez on Sat, 24 May 2003 (MIT)
PHP recipes (51)
Alvin Estevez's recipes (2)

Required Modules

  • (none specified)

Other Information and Tasks