cfscript
// Returns if the provided value is a signed integer up to 32 Bit.
function isINT(any value) {
return (
isSimpleValue(ARGUMENTS.value) &&
(reFind("^\-?[0-9]{1,10}$", ARGUMENTS.value) > 0) &&
(ARGUMENTS.value <= 2147483647) &&
(ARGUMENTS.value >= -2147483648)
);
}
cftag
<cffunction name="isINT" access="public" output="false" returnType="boolean"
hint="Returns if the provided value is a signed integer up to 32 Bit.">
<cfargument name="value" type="any" required="true">
<cfreturn (
isSimpleValue(ARGUMENTS.value) and
(reFind("^\-?[0-9]{1,10}$", ARGUMENTS.value) gt 0) and
(ARGUMENTS.value lte 2147483647) and
(ARGUMENTS.value gte -2147483648)
)>
</cffunction>
isSimpleValue
making sure the input is a primitive type (by CF means), because all numbers are considered simple values in CF (string conversion)
reFind
regular expression checking digits-only (with or without sign), minimum of one digit, maximum of ten digits (implicit call of toString
here)
- check the range, all numeric types fit into 4 Bytes, thus no need to "upgrade" the type (as you would need to with BigInteger, BigDecimal etc.)
If you don't need the range check for 4 Byte integers, @DanBracuk posted an answer with a function that performs around 5-6 times faster than this one.