# CFLib.org – Common Function Library Project

## decimalRound(numberToRound, numberOfPlaces[, mode])

##### Last updated March 3, 2006

Version: 1 | Requires: CF6 | Library: MathLib

Description:
Rounds a number to a specific number of decimal places by using Java's math library. This UDF provides &quot;finer grain&quot; rounding functionality that CF does not provide with it's built-in functions. Thanks to Christian Cantrell's blog for this one. The mods are: up = rounds up, down = rounds down, even = if the number to the right of the number of decimal places (i.e. the first digit of the discarded numbers) is even, the udf rounds down. if the number to the right of the number of decimal places is odd, the udf rounds up. This take helps eliminates cumulative round errors when working with a series of calculations. Again, thanks to Christian Cantrell's for the original code which I modified.

Return Values:
Returns a number.

Example:

``````<cfoutput>
#decimalRound(125.2525152, 1)#<br/>
#decimalRound(125.2525152, 2, "up")#<br/>
#decimalRound(125.2525152, 3, "down")#<br/>
</cfoutput>``````

Parameters:

Name Description Required
numberToRound The number to round. Yes
numberOfPlaces The number of decimal places. Yes
mode The rounding mode. Defaults to even. No

Full UDF Source:

``````/**
* Rounds a number to a specific number of decimal places by using Java's math library.
*
* @param numberToRound 	 The number to round. (Required)
* @param numberOfPlaces 	 The number of decimal places. (Required)
* @param mode 	 The rounding mode. Defaults to even. (Optional)
* @return Returns a number.
* @author Peter J. Farrell (pjf@maestropublishing.com)
* @version 1, March 3, 2006
*/
function decimalRound(numberToRound, numberOfPlaces) {
// Thanks to the blog of Christian Cantrell for this one
var bd = CreateObject("java", "java.math.BigDecimal");
var mode = "even";
var result = "";

if (ArrayLen(arguments) GTE 3) {
mode = arguments[3];
}

bd.init(arguments.numberToRound);
if (mode IS "up") {
bd = bd.setScale(arguments.numberOfPlaces, bd.ROUND_HALF_UP);
} else if (mode IS "down") {
bd = bd.setScale(arguments.numberOfPlaces, bd.ROUND_HALF_DOWN);
} else {
bd = bd.setScale(arguments.numberOfPlaces, bd.ROUND_HALF_EVEN);
}
result = bd.toString();

if(result EQ 0) result = 0;

return result;
}``````

date2ExcelDate
May 5, 2016

CapFirst
April 25, 2016