CFLib.org – Common Function Library Project

CommaFormat(inNum [, default])

Last updated August 26, 2002

Version: 1 | Requires: ColdFusion 5 | Library: StrLib

 
Rated 0 time(s). Average Rating: 0

Description:
Adds commas after every third non-ending digit to the left of the decimal point. Does not alter anything to the right of the decimal point nor does it do any additional formatting. If value passed is not a number then returns the raw string, or the specified optional default value. Additionally, since numbers with leading and trailing spaces are still considered valid by IsNumeric(), this function preserves these spaces as well. Numbers in scientific notation also work fine with this function.

Return Values:
Returns a string.

Example:

view plain print about
<cfoutput>

CommaFormat("123456789"):<br>
#CommaFormat("123456789")#<br>
<br>
CommaFormat("1234.123456"):<br>
#CommaFormat("1234.123456")#<br>
<br>
CommaFormat("-1234"):<br>
#CommaFormat("-1234")#<br>
<br>
CommaFormat("+1234"):<br>
#CommaFormat("+1234")#<br>
<br>
CommaFormat("123"):<br>
#CommaFormat("123")#<br>
<br>
CommaFormat("1234.1234E-5"):<br>
#CommaFormat("1234.1234E-5")#<br>
<br>
CommaFormat("(see note)"):<br>
#CommaFormat("(see note)")#<br>
<br>
CommaFormat(".", "not a number"):<br>
#CommaFormat(".", "not a number")#<br>
<br>
CommaFormat("$1", "not a number"):<br>
#CommaFormat("$1", "not a number")#<br>
<br>
CommaFormat("1%", "not a number"):<br>
#CommaFormat("1%", "not a number")#<br>
<br>
CommaFormat("1,234", "not a number"):<br>
#CommaFormat("1,234", "not a number")#<br>

</cfoutput>

Parameters:

Name Description Required
inNum Number to format. Yes
default If number isn't numeric, display default instead. No

Full UDF Source:

view plain print about
<cfscript>
/**
 * Adds commas after every third non-ending digit to the left of the decimal point.
 * 
 * @param inNum      Number to format. (Required)
 * @param default      If number isn't numeric, display default instead. (Optional)
 * @return Returns a string. 
 * @author Shawn Seley (shawnse@aol.com) 
 * @version 1, August 26, 2002 
 */

function CommaFormat(inNum) {
    var outStr  = "";
    var decStr  = "";

    var default_value = inNum;
    if(ArrayLen(Arguments) GTE 2) default_value = Arguments[2];

    if (not IsNumeric(inNum)) {
        return (default_value);
    } else {
        if(ListLen(inNum, "."GT 1) {
            outStr = ListFirst(inNum, ".");
            decStr = "." & ListLast(inNum, ".");
        } else if (Find("."Trim(inNum)) EQ 1) {
            decStr = inNum;
        } else {
            outStr = inNum;
        }
        if (Trim(outStr) NEQ ""{
            outStr = Reverse(outStr);
            outStr = REReplace(outStr, "([0-9][0-9][0-9])""\1,""ALL");
            outStr = REReplace(outStr, ",$""");   // delete potential leading comma
            outStr = REReplace(outStr, ",([^0-9]+)""\1");   // delete leading comma w/ spaces in front of
            outStr = Reverse(outStr);
        }
        return (outStr & decStr);
    }
}
</cfscript>
blog comments powered by Disqus

Search CFLib.org


Latest Additions

Tayo Akinmade Tayo Akinmade added
arrayTrim
10 day(s) ago

Will Belden Will Belden added
longTime
16 day(s) ago

James Sleeman James Sleeman added
quickSort
26 day(s) ago

Ben Forta Ben Forta added
GetHostAddress
29 day(s) ago

Top Rated

Darwan Leonardo Sitepu EksporSQLData
Rated 5.0, 16 time(s)

Darwan Leonardo Sitepu backupDatabase
Rated 5.0, 13 time(s)

Barney Boisvert indentXml
Rated 5.0, 10 time(s)

Kevin Pepperman generateSsccAsn
Rated 5.0, 4 time(s)

Created by Raymond Camden / Design by Justin Johnson