DollarFormat2(inNum [, default_var])
Last updated September 16, 2002
Version: 1 | Requires: ColdFusion 5 | Library: StrLib
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:
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:
<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>
Search CFLib.org
Latest Additions
Dave Anderson added
iniToStruct
20 day(s) ago
Dave Anderson added
deDupeArray
20 day(s) ago
Richard added
dice
22 day(s) ago
Isaac Dealey added
getRelative
a while ago
Top Rated
backupDatabase
Rated 5.0, 22 time(s)
indentXml
Rated 5.0, 10 time(s)
generateSsccAsn
Rated 5.0, 4 time(s)
highlightAndCrop
Rated 5.0, 4 time(s)