CFLib.org – Common Function Library Project

GetNumbers(textStr[, allowDec])

Last updated December 18, 2001

author

Mark Andrachek

Version: 1 | Requires: CF5 | Library: StrLib

Description:
Returns the passed string with all non-numbers removed (letters, punctuation, whitespace, etc.). If an optional second argument is defined, decimals will be allowed. Otherwise, only digits 0-9 are returned.

Return Values:
Returns a number.

Example:

<cfset mystring1="$12.42">
<cfset mystring2="(555) 555-5555">

<cfoutput>
#GetNumbers(mystring1,1)#
<br>
#GetNumbers(mystring2)#
</cfoutput>

Parameters:

Name Description Required
textStr String containing numbers you want returned. Yes
allowDec Boolean (yes/no) indicating whether to preserve decimal points. Default is No. No

Full UDF Source:

/**
 * Returns the passed string with all non-numbers removed (letters, punctuation, whitespace, etc.).
 * 
 * @param textStr      String containing numbers you want returned. 
 * @param allowDec      Boolean (yes/no) indicating whether to preserve decimal points.  Default is No. 
 * @return Returns a number. 
 * @author Mark Andrachek (hallow@webmages.com) 
 * @version 1, December 18, 2001 
 */
function GetNumbers(textstr) {
  if (arraylen(arguments) GTE 2) { 
    return REReplace(textstr,"[^0-9\.]",'','ALL'); }
  else { 
    return REReplace(textstr,"[^0-9]",'','ALL');  }
}

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