CFLib.org – Common Function Library Project

DollarFormat2(inNum [, default_var])

Last updated September 16, 2002

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

 
Rated 2 time(s). Average Rating: 3.5

Description:
Works like the built-in function DollarFormat, but does no rounding so that you can round as you see fit. Most frequently useful for displaying whole dollar amounts. Adds commas to every third digit to the left of the decimal point. Uses parenthesis to denote negative values. And of course adds a dollar sign. If value passed is not a number then returns the raw string, or the specified optional default value. Incidentally, numbers in scientific notation also work with this function.

Return Values:
Returns a string.

Example:

view plain print about
<cfoutput>
DollarFormat2("123456789"):<br>
#DollarFormat2("123456789")#<br>
<br>
DollarFormat2("1234.123456"):<br>
#DollarFormat2("1234.123456")#<br>
<br>
DollarFormat2("-1234"):<br>
#DollarFormat2("-1234")#<br>
<br>
DollarFormat2("+1234"):<br>
#DollarFormat2("+1234")#<br>
<br>
DollarFormat2("123"):<br>
#DollarFormat2("123")#<br>
<br>
DollarFormat2("(see note)"):<br>
#DollarFormat2("(see note)")#<br>
<br>
DollarFormat2(".", "not a number"):<br>
#DollarFormat2(".", "not a number")#<br>
<br>
DollarFormat2("$1", "not a number"):<br>
#DollarFormat2("$1", "not a number")#<br>
<br>
DollarFormat2("1%", "not a number"):<br>
#DollarFormat2("1%", "not a number")#<br>
<br>
DollarFormat2("1,234", "not a number"):<br>
#DollarFormat2("1,234", "not a number")#<br>
</cfoutput>

Parameters:

Name Description Required
inNum Number to format. Yes
default_var Value to use if number isn't a proper number. No

Full UDF Source:

view plain print about
<cfscript>
/**
 * Works like the built-in function DollarFormat, but does no rounding.
 * 
 * @param inNum      Number to format. (Required)
 * @param default_var      Value to use if number isn't a proper number. (Optional)
 * @return Returns a string. 
 * @author Shawn Seley (shawnse@aol.com) 
 * @version 1, September 16, 2002 
 */

function DollarFormat2(inNum) {
    var out_str             = "";
    var decimal_str         = "";

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

    if (not IsNumeric(inNum)) {
        return (default_value);
    } else {
        inNum = Trim(inNum);
        if(ListLen(inNum, "."GT 1) {
            out_str = Abs(ListFirst(inNum, "."));
            decimal_str = "." & ListLast(inNum, ".");
        } else if (Find(".", inNum) EQ 1) {
            decimal_str = inNum;
        } else {
            out_str = Abs(inNum);
        }
        if (out_str NEQ ""{
            // add commas
            out_str = Reverse(out_str);
            out_str = REReplace(out_str, "([0-9][0-9][0-9])""\1,""ALL");
            out_str = REReplace(out_str, ",$""");   // delete potential leading comma
            out_str = Reverse(out_str);
        }

        // add dollar sign (and parenthesis if negative)
        if(inNum LT 0) {
            return ("($" & out_str & decimal_str & ")");
        } else {
            return ("$" & out_str & decimal_str);
        }
    }
}
</cfscript>
blog comments powered by Disqus

Search CFLib.org


Latest Additions

Dave Anderson Dave Anderson added
iniToStruct
20 day(s) ago

Dave Anderson Dave Anderson added
deDupeArray
20 day(s) ago

Richard Richard added
dice
22 day(s) ago

Isaac Dealey Isaac Dealey added
getRelative
a while ago

Top Rated

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

Barney Boisvert indentXml
Rated 5.0, 10 time(s)

Kevin Pepperman generateSsccAsn
Rated 5.0, 4 time(s)

Raymond Camden highlightAndCrop
Rated 5.0, 4 time(s)

Created by Raymond Camden / Design by Justin Johnson