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;        
    }     
}

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

Created by Raymond Camden / Design by Justin Johnson