CFLib.org – Common Function Library Project

trimZeros(num)

Last updated August 26, 2012

author

Alan McCollough

Version: 1 | Requires: CF5 | Library: StrLib

Description:
I take a number, and if it equals zero, I return zero. Otherwise, I return the number, stripped of leading and trailing zeros. Based on a modification of Ray Camden's TrimZero()

Return Values:
A string with leading and trailing zeros removed

Example:

trimZeros("0") = 0
trimZeros("0.01") = .01
trimZeros("0.50") = .5
trimZeros("1.0") = 1
trimZeros("1.50") = 1.5
trimZeros("1.51") = 1.51

Parameters:

Name Description Required
num String to trim zeros from Yes

Full UDF Source:

/**
 * I trim leading and trailing zeros off of a decimal number.
 * version 1.0 by Alan McCollough
 * 
 * @param num 	 String to trim zeros from (Required)
 * @return A string with leading and trailing zeros removed 
 * @author Alan McCollough (amccollough@anthc.org) 
 * @version 1, August 26, 2012 
 */
function trimZeros(num){	
	if(val(num) == 0){
		return "0";
	} else if (num < 1) {
		return "." & listLast(num + 0,".");	
	} else {
		return num + 0;		
	} 	
}
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