Determine if a string is numeric: Difference between revisions

Add ed example
(→‎BQN: add)
(Add ed example)
 
(10 intermediate revisions by 7 users not shown)
Line 1,383:
You entered a number.</pre>
NB: While "99, rue de Rivoli" contains a number it is not a number entirely. The Val(v$) in this case would have stopped after it converted the "99" portion of the input, which when converted back to a string and compared to the original input would not result in an equality. 9E4 the program reads as an exponential value.
 
==={{header|SmallBASIC}}===
y = isnumber(s) returns true if s is a number or can be converted to a number.
<syntaxhighlight lang="qbasic">
a = 1.2345
b = "abc"
c = "-1.2345"
 
print isnumber(a)
print isnumber(b)
print isnumber(c)
</syntaxhighlight>
 
{{out}}
<pre>
1
0
1
</pre>
 
=={{header|Batch File}}==
Line 1,531 ⟶ 1,550:
 
<syntaxhighlight lang="c">#include <ctype.h>
#include <stdbool.h>
#include <stdlib.h>
 
int isNumeric (const char * s)
bool isNumeric(const char *s) {
{
if (s == NULL || *s == '\0' || isspace(*s)) {
return 0false;
char * p;}
strtodchar (s, &*p);
strtod(s, &p);
return *p == '\0';
}</syntaxhighlight>
Line 1,572 ⟶ 1,593:
 
=={{header|C++}}==
{{trans|C}}<syntaxhighlight lang="cpp">#include <cctype>
#include <cstdlib>
 
bool isNumeric(const char *s) {
if (s == nullptr || *s == '\0' || std::isspace(*s)) {
return false;
}
char *p;
std::strtod(s, &p);
return *p == '\0';
}</syntaxhighlight>
 
Using stringstream:
<syntaxhighlight lang="cpp">#include <sstream> // for istringstream
Line 1,987 ⟶ 2,020:
→ YES
</syntaxhighlight>
 
=={{header|Ed}}==
 
<syntaxhighlight lang="sed">
H
g/^([-+]?[0-9]*)(\.[0-9]+([eE][+-]?[0-9]+)?)?$/s//\1\2 is numeric/
v/numeric/s/.*/& is not numeric/
,p
Q
</syntaxhighlight>
 
{{out}}
 
<pre>$ cat string-numeric.ed | ed -GlEs string-numeric.input
Newline appended
56233 is numeric
-315 is numeric
1.36 is numeric
-5.126 is numeric
3.7E-05 is numeric
1.23BC is not numeric
5.6.3 is not numeric</pre>
 
=={{header|Elixir}}==
Line 2,003 ⟶ 2,058:
<pre>
["123", "-12.3", "-12e5", "+123"]
</pre>
 
=={{header|Emacs Lisp}}==
<syntaxhighlight lang="lisp">
(defun string-valid-number-p (str)
"Test if STR is a numeric string.
Eliminate strings with commas in them because ELisp numbers do
not contain commas. Then check if remaining strings would be
valid ELisp numbers if the quotation marks were removed."
(and
;; no comma in string, because ELisp numbers do not have commas
;; we need to eliminate any string with a comma, because the
;; numberp function below will not weed out commas
(not (string-match-p "," str))
;; no errors from numberp function testing if a number
(ignore-errors (numberp (read str)))))
 
 
</syntaxhighlight>
{{out}}
Below is ELisp code to test two lists. One is a list of strings that
would be acceptable numbers in Emacs ELisp. The second is a list of strings
that are not valid numbers. The code tests each string in each list.
<syntaxhighlight lang="lisp">
(setq valid-strings '("3" "0" "-0" "2." "1000" "-4" "-5." "6.2" "-8.45" "+15e2" "-15e2" "#b101100" "#o54" "#x2c" "1500.0" "#24r1k" "3"))
 
(setq invalid-strings '("3cat" "1,000" "5.6.7" "cat3" "def" "zero"))
 
(with-current-buffer (pop-to-buffer "my-test")
(erase-buffer)
(insert "Test for valid strings:\n")
(dolist (test-string valid-strings)
(let ((test-result))
(setq test-result (string-valid-number-p test-string))
(insert (format "%-8s - %s \n" test-string test-result))))
(insert "\n" "\n")
(insert "Test for invalid strings:\n")
(dolist (test-string invalid-strings)
(let ((test-result))
(setq test-result (string-valid-number-p test-string))
(insert (format "%-5s - %s \n" test-string test-result)))))
</syntaxhighlight>
Below is the result of the tests:
<pre>
Test for valid strings:
3 - t
0 - t
-0 - t
2. - t
1000 - t
-4 - t
-5. - t
6.2 - t
-8.45 - t
+15e2 - t
-15e2 - t
#b101100 - t
#o54 - t
#x2c - t
1500.0 - t
#24r1k - t
3 - t
 
 
Test for invalid strings:
3cat - nil
1,000 - nil
5.6.7 - nil
cat3 - nil
def - nil
zero - nil
</pre>
 
Line 2,814 ⟶ 2,940:
 
=={{header|jq}}==
'''Works with jq, the C implementation of jq'''
In versions of jq that support try/catch, the simplest way to test if a string can be parsed as a number is:<syntaxhighlight lang="jq">try tonumber catch false</syntaxhighlight>
 
'''Works with gojq, the Go implementation of jq'''
 
'''Works with jaq, the Rust implementation of jq'''
 
The simplest way to test if a string can be parsed as a number is:<syntaxhighlight lang="jq">try tonumber catch false</syntaxhighlight>
The above expression will emit the corresponding number, or false if there is none. Here then is a boolean filter which will also emit true for each input that is a number:
<syntaxhighlight lang="jq">def is_numeric: true and try tonumber catch false;</syntaxhighlight>
Line 3,685 ⟶ 3,817:
=={{header|Pascal}}==
See [[#Delphi|Delphi]] or [[#Free Pascal|Free Pascal]].
 
 
=={{header|PascalABC.NET}}==
<syntaxhighlight lang="pascal">
function IsNumeric(s: string): boolean;
begin
var i: integer;
Result := integer.TryParse(s,i)
end;
 
begin
var s := '123';
if IsNumeric(s) then
Print('string is numeric')
end.
</syntaxhighlight>
 
=={{header|PeopleCode}}==
Line 4,449 ⟶ 4,597:
 
=={{header|Rust}}==
<syntaxhighlight lang="rust">// This function is not limited to just numeric types but rather anything that implements the FromStr trait.
// This function is not limited to just numeric types but rather anything that implements the FromStr trait.
fn parsable<T: FromStr>(s: &str) -> bool {
s.parse::<T>().is_ok()
}
}</syntaxhighlight>
</syntaxhighlight>
 
Sample code:
 
<syntaxhighlight lang="rust">
use std::str::FromStr;
 
fn parsable<T: FromStr>(s: &str) -> bool {
s.parse::<T>().is_ok()
}
 
fn main() {
let test_cases = [
"142857",
"3.14",
"not of this earth!"
];
 
let types: &[(&str, fn(&str) -> bool)] = &[
("i32", parsable::<i32> as fn(&str) -> bool),
("i64", parsable::<i32> as fn(&str) -> bool),
("i128", parsable::<i32> as fn(&str) -> bool),
("f64", parsable::<f64> as fn(&str) -> bool),
];
 
println!("");
for &case in &test_cases {
for &(type_name, parse_fn) in types {
println!(
"'{}' {} be parsed as {}",
case,
if parse_fn(case) { "can" } else { "_cannot_" },
type_name
);
}
println!("");
}
}
</syntaxhighlight>
 
{{out}}
<pre>
'142857' can be parsed as i32
'142857' can be parsed as i64
'142857' can be parsed as i128
'142857' can be parsed as f64
 
'3.14' _cannot_ be parsed as i32
'3.14' _cannot_ be parsed as i64
'3.14' _cannot_ be parsed as i128
'3.14' can be parsed as f64
 
'not of this earth!' _cannot_ be parsed as i32
'not of this earth!' _cannot_ be parsed as i64
'not of this earth!' _cannot_ be parsed as i128
'not of this earth!' _cannot_ be parsed as f64
</pre>
 
=={{header|Scala}}==
Line 5,407 ⟶ 5,614:
 
</pre>
 
=={{header|Zig}}==
<syntaxhighlight lang="zig">
const std = @import("std");
const stdout = std.io.getStdOut().writer();
 
fn isNumeric(str: []const u8) bool {
const num = std.mem.trim(u8, str, "\x20");
_ = std.fmt.parseFloat(f64, num) catch return false;
return true;
}
 
pub fn main() !void {
const s1 = " 123";
const s2 = " +123";
const s3 = " 12.3";
const s4 = "-12.3";
const s5 = "12e-3";
const s6 = "=12-3";
const s7 = "abcde";
const s8 = "12cde";
const s9 = "NaN";
const s10 = "0xFF";
 
try stdout.print("Is {s} numeric? {}\n", .{ s1, isNumeric(s1) });
try stdout.print("Is {s} numeric? {}\n", .{ s2, isNumeric(s2) });
try stdout.print("Is {s} numeric? {}\n", .{ s3, isNumeric(s3) });
try stdout.print("Is {s} numeric? {}\n", .{ s4, isNumeric(s4) });
try stdout.print("Is {s} numeric? {}\n", .{ s5, isNumeric(s5) });
try stdout.print("Is {s} numeric? {}\n", .{ s6, isNumeric(s6) });
try stdout.print("Is {s} numeric? {}\n", .{ s7, isNumeric(s7) });
try stdout.print("Is {s} numeric? {}\n", .{ s8, isNumeric(s8) });
try stdout.print("Is {s} numeric? {}\n", .{ s9, isNumeric(s9) });
try stdout.print("Is {s} numeric? {}\n", .{ s10, isNumeric(s10) });
}
</syntaxhighlight>
 
{{out}}
<pre>
Is 123 numeric? true
Is +123 numeric? true
Is 12.3 numeric? true
Is -12.3 numeric? true
Is 12e-3 numeric? true
Is =12-3 numeric? false
Is abcde numeric? false
Is 12cde numeric? false
Is NaN numeric? true
Is 0xFF numeric? true
</pre>
 
 
 
=={{header|zkl}}==
104

edits