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