CFLib.org – Common Function Library Project

TrailingZeroes(inNum, places)

Last updated September 27, 2002

author

Shawn Seley

Version: 1 | Requires: CF5 | Library: MathLib

Description:
Restores significant trailing zeroes which may have been omitted during calculations. Does not remove excess trailing zeroes (use a rounding function for this).

Return Values:
Returns a number.

Example:

<cfoutput>
10.0/2:<br>
#10.0/2#<br>
<br>
TrailingZeroes(10.0/2, 1):<br>
#TrailingZeroes(10.0/2, 1)#<br>
<br>
TrailingZeroes(5.0/2, 1):<br>
#TrailingZeroes(5.0/2, 1)#<br>
<br>
TrailingZeroes(5.00, 1):<br>
#TrailingZeroes(5.00, 1)#<br>
</cfoutput>

Parameters:

Name Description Required
inNum The number. Yes
places Number of significant digits. Yes

Full UDF Source:

/**
 * Restores significant trailing zeroes which may have been omitted during calculations.
 * 
 * @param inNum 	 The number. (Required)
 * @param places 	 Number of significant digits. (Required)
 * @return Returns a number. 
 * @author Shawn Seley (shawnse@aol.com) 
 * @version 1, September 27, 2002 
 */
function TrailingZeroes(inNum, decPlaces){
	var existing_dec_places  = 0;

	if(decPlaces GT 0) {
		if(inNum contains "."){
			existing_dec_places = Len(ListGetAt(inNum, 2, "."));
			if(existing_dec_places LT decPlaces){
				return inNum & RepeatString("0", decPlaces - existing_dec_places);
			}
		} else {
			// was shortened to an integer without a decimal point
			return inNum & "." & RepeatString("0", decPlaces - existing_dec_places);
		}
	}
	return inNum;
}
blog comments powered by Disqus

Search CFLib.org


Latest Additions

Kevin Cotton added
date2ExcelDate
May 5, 2016

Raymond Camden added
CapFirst
April 25, 2016

Chris Wigginton added
loremIpsum
January 18, 2016

Gary Stanton added
calculateArrival...
November 19, 2015

Sebastiaan Naafs - van Dijk added
getDaysInQuarter
November 13, 2015

Created by Raymond Camden / Design by Justin Johnson