Determine if a string is numeric

Determine if a string is numeric
You are encouraged to solve this task according to the task description, using any language you may know.

Task

Create a boolean function which takes in a string and tells whether it is a numeric string (floating point and negative numbers included) in the syntax the language uses for numeric literals or numbers converted from strings.

% there is at least one non-blank character % % must have either an integer or real/immaginary number % % integer: [+|-]digit-sequence % % real: [+|-][digit-sequence].digit-sequence['integer][L] % % or: [+|-]digit-sequence[.[digit-sequence]]'integer[L] % % imaginary: % % [+|-][digit-sequence].digit-sequence['integer][L]I% % or: [+|-]digit-sequence[.[digit-sequence]]'integer[L]I% % The "I" at the end of an imaginary number can appear % % before or after the "L" (which indicates a long number) % % the "I" and "L" can be in either case %

" " is not numeric as expected
"b " is not numeric as expected
". " is not numeric as expected
".'3 " is not numeric as expected
"3.' " is not numeric as expected
"0.0z44 " is not numeric as expected
"-1IL " is not numeric as expected
"4.5'23ILL " is not numeric as expected
---------
"-1 " is numeric as expected
" +.345 " is numeric as expected
"4.5'23I " is numeric as expected
"-5'+3i " is numeric as expected
"-5'-3l " is numeric as expected
" -.345LI " is numeric as expected

System.debug(numericString.isNumeric()); // this will be trueSystem.debug(partlyNumericString.isNumeric()); // this will be falseSystem.debug(decimalString.isNumeric()); // this will be falseSystem.debug(decimalString.remove('.').isNumeric()); // this will be true

AutoHotkey has no explicitly defined variable types. A variable containing only digits (with an optional decimal point) is automatically interpreted as a number when a math operation or comparison requires it.

'PI' is NOT a number
'0123' is a number
'-0123' is a number
'12.30' is a number
'-12.30' is a number
'123!' is NOT a number
'0' is a number
'0.0' is a number
'.123' is a number
'-.123' is a number
'12E3' is a number
'12E-3' is a number
'12+3' is NOT a number
'end' is NOT a number

To check whether a string is a number, a fraction or an integer, use the patterns #, / and ~/# ("not a fraction and yet a number"). In the pattern matching examples below (which can be typed in at the Bracmat prompt) F denotes 'failure' and S denotes 'success'.

43257349578692:/ F

260780243875083/35587980:/ S

247/30:~/# F

80000000000:~/# S

Bracmat doesn't do floating point computations (historical reason: the Acorn Risc Machine a.k.a. ARM processor in the Archimedes computer did not have an FPU), but the pattern ~/# (|"." (|? 0|`) (|~/#:>0)) (|(E|e) ~/#) recognises string representations of floating point numbers.

@("1.000-4E-10":~/# (|"." (|? 0|`) (|~/#:>0)) (|(E|e) ~/#)) F

@("1.0004E-54328":~/# (|"." (|? 0|`) (|~/#:>0)) (|(E|e) ~/#)) S

@("-464641.0004E-54328":~/# (|"." (|? 0|`) (|~/#:>0)) (|(E|e) ~/#)) S

@("1/2.0004E-10":~/# (|"." (|? 0|`) (|~/#:>0)) (|(E|e) ~/#)) F

@("1357E-10":~/# (|"." (|? 0|`) (|~/#:>0)) (|(E|e) ~/#)) S

@("1357e0":~/# (|"." (|? 0|`) (|~/#:>0)) (|(E|e) ~/#)) S

@("13579":~/# (|"." (|? 0|`) (|~/#:>0)) (|(E|e) ~/#)) S

@("1.246":~/# (|"." (|? 0|`) (|~/#:>0)) (|(E|e) ~/#)) S

@("0.0":~/# (|"." (|? 0|`) (|~/#:>0)) (|(E|e) ~/#)) S

@("0.0000":~/# (|"." (|? 0|`) (|~/#:>0)) (|(E|e) ~/#)) S

To do computations with such "floating point strings" you would have to convert such strings to fractional representations first.

COBOL has the intrinsic functions TEST-NUMVAL and TEST-NUMVAL-C to check if a string is numeric (TEST-NUMVAL-C is used to check if it is also a monetary string). Implementations supporting the 20XX draft standard can also use TEST-NUMVAL-F for floating-point numbers. They return 0 if the string is valid, or the position of the first incorrect character.

ignore-errors here handles returning nil in case the input is invalid rather than simply non-numeric.

However, read[-from-string] has the side effect of interning any symbols encountered, and can have memory allocation larger than the input size (due to read syntax such as #*, which takes a length). The parse-number library provides a numbers-only equivalent of read.

Erlang doesn't come with a way to say if a string represents a numeric value or not, but does come with the built-in function is_number/1, which will return true if the argument passed is either an integer or a float. Erlang also has two functions to transform a string to either a floating number or an integer, which will be used in conjunction with is_number/1.

The 'fromStr' methods return a parsed number or given an error. The 'false' tells each method to return null if the string does not parse as a number of given type, otherwise, the 'fromStr' method throws an exception.

FreeBASIC has a built-in Val() function which converts numeric strings to doubles. However, it is not ideal for the present task
since it will try to convert as much of the string as it can (so "123xyz" would convert to 123) and return 0 if a conversion on
this basis is not possible (i.e. "xyz" would return 0).

I've therefore written a custom function which recognizes signed numbers in bases from 2 to 16 (but only integral numbers for bases other than 10). For base 10, it will treat "123." or ".123" as numbers but not ".". It doesn't recognize scientific notation but does recognize the integral prefixes &H, &O and &B if bases 16, 8 or 2 respectively are specified.

This function is not particularly useful in a statically typed language. Instead, one would just attempt to convert the string
to the desired type with read or reads, and handle parsing failure appropriately.

The task doesn't define which strings are considered "numeric", so we do Integers and Doubles, which should catch the most common cases (including hexadecimal 0x notation):

The code writes a printable image of x whatever type it is and a statement about whether it is numeric or not. Icon and Unicon use success and failure instead of boolean functions, numeric(x) is built-in and returns x or fails.

It's generally bad practice in Java to rely on an exception being thrown since exception handling is relatively expensive. If non-numeric strings are common, you're going to see a huge performance hit.

Alternative 3 : use the positional parser in the java.text.NumberFormat object (a more robust solution). If, after parsing, the parse position is at the end of the string, we can deduce that the entire string was a valid number.

Scanner also has similar methods for longs, shorts, bytes, doubles, floats, BigIntegers, and BigDecimals as well as methods for integral types where you may input a base/radix other than 10 (10 is the default, which can be changed using the useRadix method).

The function isnumeric tests for strings that parse directly to numbers. This test excludes symbols, such as π and Inf, that evaluate to numbers as well as certain elaborate numbers (large integers, rationals and complex numbers) whose literals parse to expressions that must be evaluated to yield numbers. Testing strings that evaluate to Number following parsing would cast too wide a net by identifying strings such as "1 + 1" as numbers.

1 is a direct numeric literal.
-121 is a direct numeric literal.
one is not a direct numeric literal.
pi is not a direct numeric literal.
1 + 1 is not a direct numeric literal.
NaN is not a direct numeric literal.
1234567890123456789 is a direct numeric literal.
1234567890123456789123456789 is not a direct numeric literal.
1234567890123456789123456789.0 is a direct numeric literal.
1.3 is a direct numeric literal.
1.4e10 is a direct numeric literal.
Inf is not a direct numeric literal.
1//2 is not a direct numeric literal.
1.0 + 1.0im is not a direct numeric literal.

This image is a VI Snippet, an executable image of LabVIEW code. The LabVIEW version is shown on the top-right hand corner. You can download it, then drag-and-drop it onto the LabVIEW block diagram from a file browser, and it will appear as runnable, editable code.

'assume it is number and try to prove wrong IsNumber =1for i =1tolen(string$)selectcasemid$(string$, i,1)case"0","1","2","3","4","5","6","7","8","9" HasNumeric =1'to check if there are any digitscase"e","E"'"e" must not occur more than once'must not occur before digits

if HasE >0or HasNumeric =0then IsNumber =0exitforendif HasE = i 'store position of "e" HasNumeric =0'needs numbers after "e"case"-","+"'must be either first character or immediately after "e"'(HasE = 0 if no occurrences yet)if HasE <> i-1then IsNumber =0exitforendifcase"."'must not have previous points and must not come after "e"if HasE <>0or HasPoint <>0then IsNumber =0exitforendif HasPoint =1caseelse'no other characters allowed IsNumber =0exitforendselectnext i'must have digitsif HasNumeric =0then IsNumber =0[NotNumber]endfunction

puts '123 is numeric' if is_numeric?('123')puts '-123 is numeric' if is_numeric?('-123')puts '123.1 is numeric' if is_numeric?('123.1')

puts 'nil is not numeric' unless is_numeric?(nil)puts "'' is not numeric" unless is_numeric?('')puts 'abc is not numeric' unless is_numeric?('abc')puts '123- is not numeric' unless is_numeric?('123-')puts '1.2.3 is not numeric' unless is_numeric?('1.2.3')

# check every element of the stringdef is_numeric2?(s: string) if (s == nil || s.isEmpty()) return false end if (!s.startsWith('-')) if s.contains('-') return false end end

puts '123 is numeric' if is_numeric2?('123')puts '-123 is numeric' if is_numeric2?('-123')puts '123.1 is numeric' if is_numeric2?('123.1')

puts 'nil is not numeric' unless is_numeric2?(nil)puts "'' is not numeric" unless is_numeric2?('')puts 'abc is not numeric' unless is_numeric2?('abc')puts '123- is not numeric' unless is_numeric2?('123-')puts '1.2.3 is not numeric' unless is_numeric2?('1.2.3')

puts '123 is numeric' if is_numeric3?('123')puts '-123 is numeric' if is_numeric3?('-123')puts '123.1 is numeric' if is_numeric3?('123.1')

puts 'nil is not numeric' unless is_numeric3?(nil)puts "'' is not numeric" unless is_numeric3?('')puts 'abc is not numeric' unless is_numeric3?('abc')puts '123- is not numeric' unless is_numeric3?('123-')puts '1.2.3 is not numeric' unless is_numeric3?('1.2.3')

# use the positional parser in the java.text.NumberFormat object # (a more robust solution). If, after parsing, the parse position is at # the end of the string, we can deduce that the entire string was a # valid number.def is_numeric4?(s:string) return false if s == nil formatter = NumberFormat.getInstance() pos = ParsePosition.new(0) formatter.parse(s, pos) s.length() == pos.getIndex()end

puts '123 is numeric' if is_numeric4?('123')puts '-123 is numeric' if is_numeric4?('-123')puts '123.1 is numeric' if is_numeric4?('123.1')

puts 'nil is not numeric' unless is_numeric4?(nil)puts "'' is not numeric" unless is_numeric4?('')puts 'abc is not numeric' unless is_numeric4?('abc')puts '123- is not numeric' unless is_numeric4?('123-')puts '1.2.3 is not numeric' unless is_numeric4?('1.2.3')

# use the java.util.Scanner object. Very useful if you have to # scan multiple entries. Scanner also has similar methods for longs, # shorts, bytes, doubles, floats, BigIntegers, and BigDecimals as well # as methods for integral types where you may input a base/radix other than # 10 (10 is the default, which can be changed using the useRadix method).def is_numeric5?(s:string) return false if s == nil Scanner sc = Scanner.new(s) sc.hasNextDouble()end

puts '123 is numeric' if is_numeric5?('123')puts '-123 is numeric' if is_numeric5?('-123')puts '123.1 is numeric' if is_numeric5?('123.1')

puts 'nil is not numeric' unless is_numeric5?(nil)puts "'' is not numeric" unless is_numeric5?('')puts 'abc is not numeric' unless is_numeric5?('abc')puts '123- is not numeric' unless is_numeric5?('123-')puts '1.2.3 is not numeric' unless is_numeric5?('1.2.3')

In MUMPS, strings are automatically converted to numbers when a unary or binary arithmetic operator works upon them. If there are no leading digits, a string converts to zero. If there a string of digits followed by an "e" or an "E" followed in turn by more digits, the numbers after the letter are treated as an exponent.

The NSScanner class supports scanning of strings for various types. The scanFloat method will return YES if the string is numeric, even if the number is actually too long to be contained by the precision of a float.

if([[NSScanner scannerWithString:@"-123.4e5"] scanFloat:NULL]) NSLog(@"\"-123.4e5\" is numeric");else NSLog(@"\"-123.4e5\" is not numeric");if([[NSScanner scannerWithString:@"Not a number"] scanFloat:NULL]) NSLog(@"\"Not a number\" is numeric");else NSLog(@"\"Not a number\" is not numeric");// prints: "-123.4e5" is numeric// prints: "Not a number" is not numeric

The following function can be used to check if a string is numeric "totally"; this is achieved by checking if the scanner reached the end of the string after the float is parsed.

If we want to scan by hand, we could use a function like the following, that checks if a number is an integer positive or negative number; spaces can appear at the beginning, but not after the number, and
the '+' or '-' can appear only attached to the number ("+123" returns YES, but "+ 123" returns NO).

Here we assumed that in the internal encoding of a string (that should be Unicode), 1 comes after 0, 2 after 1 and so on until 9. Another way could be to get the C String from the NSString object, and then the parsing would be the same of the one we could do in standard C, so this path is not given.

The builtin function isnumeric return true (1) if the argument is a data of type number; the provided function isnum works the same for numeric datatype, while if another type is passed as argument, it tries to convert it to a number; if the conversion fails, it means it is not a string representing a number.

Use the IsNumber function to determine if Value contains a valid numeric value. Numeric characters include sign indicators and comma and period decimal points.To determine if a value is a number and if it's in the user's local format, use the IsUserNumber function.

Parameters

ValueSpecify a string you want to search to determine if it is a valid number.

There are also some commonly used modules for the task. Scalar::Util (distributed with 5.8) provides access to Perl's internal function "looks_like_number" for determining whether a variable looks like a number. Data::Types exports functions that validate data types using both the above and other regular expressions. Thirdly, there is "Regexp::Common" which has regular expressions to match various types of numbers. Those three modules are available from the CPAN.

If you're on a POSIX system, Perl supports the "POSIX::strtod" function. Its semantics are somewhat cumbersome, so here's a "getnum" wrapper function for more convenient access. This function takes a string and returns the number it found, or "undef" for input that isn't a C float. The "is_numeric" function is a front end to "getnum" if you just want to say, Is this a float?

Or you could check out the String::Scanf module on the CPAN instead. The POSIX module (part of the standard Perl distribution) provides the "strtod" and "strtol" for converting strings to double and longs, respectively.

Function isNumeric(s as string, optchar as string) as integer If len(s) = 0 then Result = FALSE Exit Function End If if instr(s,"+") > 1 then Result = FALSE exit function end if if instr(s,"-") > 1 then Result = FALSE exit function end if Defint i, ndex = 0 For i = 1 to len(s) select case asc(mid$(s,i,1)) case 43 '+ case 45 '- case 46 '. if ndex = 1 then Result = FALSE Exit function end if ndex = 1 case 48 to 57 '0 to 9 case else if instr(optchar,(mid$(s,i,1))) = 0 then Result = FALSE exit function end if end select next Result = TRUEEnd Function

Returns a flag of TRUE if character-string parameter represents a signed or unsigned integer. Otherwise returns a flag of FALSE. The success or failure is dependent on the source is valid in the current numeric base. The >number function also recognizes several optional prefixes for overriding the current base during conversion.

[ ( string -- flag ) >number nip ] is isNumeric

( Some tests )decimal" 100" isNumeric . ( succeeds, 100 is a valid decimal integer )" 100.21" isNumeric . ( fails, 100.21 is not an integer)" a" isNumeric . ( fails, 'a' is not a valid integer in the decimal base )" $a" isNumeric . ( succeeds, because $ is a valid override prefix ) ( denoting that the following character is a hexadecimal number )

This routine returns TRUE if there is numeric value at current cursor location.
Only signed and unsigned integers are recognized, in decimal, hex (preceded with 0x) or octal (preceded with 0o).
Remove the SUPPRESS option to evaluate an expression instead of single numeric value.

The inbuilt function STRING->NUMBER returns the numeric value of a string if it can. We discard this value and return the Boolean value 'true'; otherwise, the IF conditional will not be satisfied and will return 'false'.