String::Numeric - Determine whether a string represents a numeric value
$boolean = is_float($string); $boolean = is_decimal($string); $boolean = is_int($string); $boolean = is_int8($string); $boolean = is_int16($string); $boolean = is_int32($string); $boolean = is_int64($string); $boolean = is_int128($string); $boolean = is_uint($string); $boolean = is_uint8($string); $boolean = is_uint16($string); $boolean = is_uint32($string); $boolean = is_uint64($string); $boolean = is_uint128($string);
A numeric value contains an integer part that may be prefixed with an optional minus sign, which may be followed by a fractional part and/or an exponent part.
See "String::Numeric ABNF" for specification and "COMPARISON" for a comparison with Scalar::Util::looks_like_number().
Scalar::Util::looks_like_number()
Determine whether $string is a floating-point number of arbitrary precision.
$string
Usage
$boolean = is_float($string); $boolean = is_float('-1'); # true $boolean = is_float('1'); # true $boolean = is_float('1.0'); # true $boolean = is_float('1.0e6'); # true $boolean = is_float('1e6'); # true $boolean = is_float(undef); # false
Arguments
Returns
$boolean
Note
is_numeric()
Determine whether $string is a decimal number of arbitrary precision.
$boolean = is_decimal($string); $boolean = is_decimal('-1'); # true $boolean = is_decimal('1'); # true $boolean = is_decimal('1.0'); # true $boolean = is_decimal('1.0e6'); # false $boolean = is_decimal('1e6'); # false $boolean = is_decimal(undef); # false
Determine whether $string is an integer of arbitrary precision.
$boolean = is_int($string); $boolean = is_int('1'); # true $boolean = is_int('-1'); # true $boolean = is_int('1.0'); # false $boolean = is_int(undef); # false
is_integer()
Determine whether $string is a 8-bit signed integer which can have any value in the range -128 to 127.
$boolean = is_int8($string); $boolean = is_int8('-128'); # true $boolean = is_int8('127'); # true $boolean = is_int8(undef); # false
Determine whether $string is a 16-bit signed integer which can have any value in the range -32,768 to 32,767.
$boolean = is_int16($string); $boolean = is_int16('-32768'); # true $boolean = is_int16('32767'); # true $boolean = is_int16(undef); # false
Determine whether $string is a 32-bit signed integer which can have any value in the range -2,147,483,648 to 2,147,483,647.
$boolean = is_int32($string); $boolean = is_int32('-2147483648'); # true $boolean = is_int32('2147483647'); # true $boolean = is_int32(undef); # false
Determine whether $string is a 64-bit signed integer which can have any value in the range -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807.
$boolean = is_int64($string); $boolean = is_int64('-9223372036854775808'); # true $boolean = is_int64('9223372036854775807'); # true $boolean = is_int64(undef); # false
Determine whether $string is a 128-bit signed integer which can have any value in the range -170,141,183,460,469,231,731,687,303,715,884,105,728 to 170,141,183,460,469,231,731,687,303,715,884,105,727.
$boolean = is_int128($string); $boolean = is_int128('-170141183460469231731687303715884105728'); # true $boolean = is_int128('170141183460469231731687303715884105727'); # true $boolean = is_int128(undef); # false
Determine whether $string is an unsigned integer of arbitrary precision.
$boolean = is_uint($string); $boolean = is_uint('1'); # true $boolean = is_uint('-1'); # false $boolean = is_uint('1.0'); # false $boolean = is_uint(undef); # false
Determine whether $string is a 8-bit unsigned integer which can have any value in the range 0 to 255.
$boolean = is_uint8($string); $boolean = is_uint8('0'); # true $boolean = is_uint8('255'); # true $boolean = is_uint8(undef); # false
Determine whether $string is a 16-bit unsigned integer which can have any value in the range 0 to 65,535.
$boolean = is_uint16($string); $boolean = is_uint16('0'); # true $boolean = is_uint16('65535'); # true $boolean = is_uint16(undef); # false
Determine whether $string is a 32-bit unsigned integer which can have any value in the range 0 to 4,294,967,295.
$boolean = is_uint32($string); $boolean = is_uint32('0'); # true $boolean = is_uint32('4294967295'); # true $boolean = is_uint32(undef); # false
Determine whether $string is a 64-bit unsigned integer which can have any value in the range 0 to 18,446,744,073,709,551,615.
$boolean = is_uint64($string); $boolean = is_uint64('0'); # true $boolean = is_uint64('18446744073709551615'); # true $boolean = is_uint64(undef); # false
Determine whether $string is a 128-bit unsigned integer which can have any value in the range 0 to 340,282,366,920,938,463,463,374,607,431,768,211,455.
$boolean = is_uint128($string); $boolean = is_uint128('0'); # true $boolean = is_uint128('340282366920938463463374607431768211455'); # true $boolean = is_uint128(undef); # false
String::Numeric supports numbers in decimal notation using Western Arabic numerals and decimal fractions seperated with a dot. Other notations or numeral systems are not supported.
String::Numeric
None of the following is considered valid by String::Numeric.
Fractional number without integer part or fractional part
looks_like_number("1.") # true looks_like_number(".1") # true
Plus prefix
looks_like_number("+1") # true
Leading zeros
looks_like_number("01") # true
Radix point other than .
.
looks_like_number("1,1") # true assuming locale is in effect and locale radix point is ','
Leading and/or trailing whitespace, such as HT, LF, FF, CR, and SP
looks_like_number("\n1") # true looks_like_number("1\n") # true looks_like_number("\n1\n") # true
Numeric values that are not represented as a sequence of digits, such as Inf, Infinity, NaN and 0 but true
Inf
Infinity
NaN
0 but true
looks_like_number("Inf") # true looks_like_number("Infinity") # true looks_like_number("NaN") # true looks_like_number("0 but true") # true
; String::Numeric ABNF Grammar is-float = [ "-" ] FloatNumber is-decimal = [ "-" ] DecimalNumber is-integer = [ "-" ] DecimalInteger is-uint = DecimalInteger FloatNumber = DecimalNumber [ ExponentPart ] DecimalNumber = DecimalInteger [ FractionalPart ] DecimalInteger = "0" / ( NonZeroDigit *DecimalDigit ) DecimalDigit = %x30-39; 0-9 NonZeroDigit = %x31-39; 1-9 FractionalPart = "." 1*DecimalDigit ExponentIndicator = "e" / "E" ExponentPart = ExponentIndicator [ "+" / "-" ] 1*DecimalDigit
; ABNF Grammar based on 5.10 Perl_grok_number() looks-like-number = Number / ZeroButTrue Number = *WS [ Sign ] ( FloatNumber / ( Infinity / NaN ) ) *WS; FloatNumber = DecimalNumber [ ExponentPart ] DecimalNumber = DecimalInteger [ "." *DecimalDigit ] / "." 1*DecimalDigit DecimalInteger = 1*DecimalDigit WS = %x09 ; HT / %x0A ; LF / %x0C ; FF / %x0D ; CR / %x20 ; SP DecimalDigit = %x30-39; 0-9 Sign = "+" / "-" ExponentIndicator = "e" / "E" ExponentPart = ExponentIndicator [ Sign ] 1*DecimalDigit Infinity = "Inf" / "Infinity" NaN = "NaN" ZeroButTrue = %x30.20.62.75.74.20.74.72.75.65 ; 0 but true
None by default. Functions and constants can either be imported individually or in sets grouped by tag names. The tag names are:
:all
:is_int
is_int
:is_uint
is_uint
Subroutine %s invoked with wrong number of arguments.
Set the environment variable STRING_NUMERIC_PP to a true value before loading this package to disable usage of XS implementation.
STRING_NUMERIC_PP
In addition to Run-Time:
String::Numeric::XS
Data::Types
Regexp::Common::number
Scalar::Util
Scalar::Util::Numeric
Christian Hansen chansen@cpan.org
chansen@cpan.org
Copyright 2009 Christian Hansen.
This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
To install String::Numeric, copy and paste the appropriate command in to your terminal.
cpanm
cpanm String::Numeric
CPAN shell
perl -MCPAN -e shell install String::Numeric
For more information on module installation, please visit the detailed CPAN module installation guide.