DollarFormat2(inNum[, default_var])
Last updated September 16, 2002
Version: 1 | Requires: CF5 | 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:
<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:
/**
* 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);
}
}
}
Search CFLib.org
Latest Additions
Raymond Camden added
QueryDeleteRows
November 04, 2017
Leigh added
nullPad
May 11, 2016
Raymond Camden added
stripHTML
May 10, 2016
Kevin Cotton added
date2ExcelDate
May 05, 2016
Raymond Camden added
CapFirst
April 25, 2016