Determine if a string is numeric: Difference between revisions

Content deleted Content added
Rob-codes (talk | contribs)
added code for testing strings
Aartaka (talk | contribs)
Add ed example
 
(6 intermediate revisions by 5 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 2,001 ⟶ 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,023 ⟶ 2,064:
(defun string-valid-number-p (str)
"Test if STR is a numeric string.
Eliminate strings with commas in them because ELipsELisp numbers do
not contain commas. Then check if remaining strings would be
valid ELisp numbers if the quotation marks were removed."
Line 3,776 ⟶ 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,540 ⟶ 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,498 ⟶ 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}}==