CFLib.org – Common Function Library Project

NumberUnFormat(inStr[, default_value])

Last updated December 23, 2002

author

Shawn Seley

Version: 1 | Requires: CF5 | Library: StrLib

Description:
Removes all non-essential formatting from a number. If no number exists then returns the raw string, or (if specified) the optional default value. Similar to Mark Andrachek's GetNumbers, but preserves minus signs and always keeps the decimal places, and provides exceptions for entirely non-numeric strings. (Note: numbers in scientific notation will not work with this function.)

Return Values:
Returns a number or string.

Example:

<cfoutput>
NumberUnFormat("-10")<br>
#NumberUnFormat("-10")#<br>
<br>
NumberUnFormat("1.5432%")<br>
#NumberUnFormat("1.5432%")#<br>
<br>
NumberUnFormat("$1,499.95")<br>
#NumberUnFormat("$1,499.95")#<br>
<br>
NumberUnFormat("down 3 points")<br>
#NumberUnFormat("down 3 points")#<br>
<br>
NumberUnFormat("&lt;b&gt;3,000,000&lt;/b&gt;")<br>
#NumberUnFormat("<b>3,000,000</b>")#<br>
<br>
NumberUnFormat("no digits")<br>
#NumberUnFormat("no digits")#<br>
<br>
NumberUnFormat("no digits", "0.00")<br>
#NumberUnFormat("no digits", "0.00")#<br>
</cfoutput>

Parameters:

Name Description Required
inStr String to format. Yes
default_value Used if the formatted string is empty. Defaults to inStr. No

Full UDF Source:

/**
 * Removes all non-essential formatting from a number.
 * 
 * @param inStr      String to format. (Required)
 * @param default_value      Used if the formatted string is empty. Defaults to inStr. (Optional)
 * @return Returns a number or string. 
 * @author Shawn Seley (shawnse@aol.com) 
 * @version 1, December 23, 2002 
 */
function NumberUnFormat(inStr) {
    var outNum = 0;
    var default_value = inStr;

    if(ArrayLen(Arguments) GTE 2) default_value = Arguments[2];

    outNum = REReplace(inStr,"[^0-9\.\-]",'','ALL');
    if (Len(outNum) LT 1) {
        outNum = default_value;
    }

    return outNum;
}

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