Integers Only, Remove Dollar Signs and Periods

Looking to remove "$" and "." (dollar signs, commas and periods/decimal points) from numbers before inserting into a database, storing integers. This means decimals need to be rounded into an integer.

ColdFusion only... We have to use ColdFusion to Round the decimals.

view plain print about
1<cfset cleanNumber = Round(Replace(Replace(DirtyNumber, "$", ""), ",", "", "ALL")) />
The first inside, Replace removes the $. The second or outer Replace removes all commas, while the Round function converts the decimal to an integer.

Here is the use of regex, regular expressions and ColdFusion:

view plain print about
1<cfset cleanNumber = Round(REReplace(DirtyNumber, "[^0-9.]", "", "ALL")) />
With this filter, the regex removes all non-numeric characters except the decimal points. The Round function does it's job rounding the closest integer.

I wanted to use only regex but regex cannot do math, i.e., round the decimal. The first statement only removed "$" and "." and ",". With JavaScript filtering on the front end the first example addresses 99% of the cases but the second is more thorough and removes all occurrences of non-numeric characters except decimal points. The only downfall to both is if there is more than one period / decimal point.

Comments (Comment Moderation is enabled. Your comment will not appear until approved.)
BlogCFC was created by Raymond Camden. This blog is running version 5.9.7. Contact Blog Owner