Determine if a string is numeric: Difference between revisions
added C translation to C++
(added C translation to C++) |
|||
(6 intermediate revisions by 5 users not shown) | |||
Line 1,469:
'192.168.0.1' Not numeric
</pre>
=={{header|BQN}}==
<syntaxhighlight lang="bqn">IsNumeric ← 1∘•ParseFloat⎊0</syntaxhighlight>
=={{header|Bracmat}}==
Line 1,484 ⟶ 1,487:
80000000000:~/#
S</syntaxhighlight>
<syntaxhighlight lang="bracmat">@("1.000-4E-10":~/# (|"." (|? 0|`) (|~/#:>0)) (|(E|e) ~/#))
F
Line 1,515 ⟶ 1,518:
S</syntaxhighlight>
Calculations with floating point numbers are delegated to UFP (Un-I-fancy-fied Floating Point, because it took me 30 years to dream up a viable way to do FP in Bracmat without breaking existing code) objects. An UFP object compiles and executes code that only handles C "doubles" and (multidimensional) arrays of such values.
=={{header|Burlesque}}==
Line 1,576 ⟶ 1,531:
<syntaxhighlight lang="c">#include <ctype.h>
#include <stdbool.h>
#include <stdlib.h>
bool isNumeric(const char *s) {
if (s == NULL || *s == '\0' || isspace(*s)) {
return
strtod(s, &p);
return *p == '\0';
}</syntaxhighlight>
Line 1,617 ⟶ 1,574:
=={{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 2,007 ⟶ 1,976:
=={{header|EasyLang}}==
<syntaxhighlight lang="text">
h = number a$
# because every variable must be used
h = h
return 1 - error
.
for s$ in [ "abc" "21a" "1234" "-13" "7.65" ]
print s$ & " is numeric"
else
print s$ & " is not numeric"
.
.
</syntaxhighlight>
Line 3,559 ⟶ 3,527:
Inf is numeric
rose is not numeric</pre>
=={{header|Nu}}==
<syntaxhighlight lang="nu">
def is-numeric [] {try {into float | true} catch {false}}
["1" "12" "-3" "5.6" "-3.14" "one" "cheese"] | each {{k: $in, v: ($in | is-numeric)}}
</syntaxhighlight>
{{out}}
<pre>
╭───┬────────┬───────╮
│ # │ k │ v │
├───┼────────┼───────┤
│ 0 │ 1 │ true │
│ 1 │ 12 │ true │
│ 2 │ -3 │ true │
│ 3 │ 5.6 │ true │
│ 4 │ -3.14 │ true │
│ 5 │ one │ false │
│ 6 │ cheese │ false │
╰───┴────────┴───────╯
</pre>
=={{header|Objeck}}==
Line 5,144 ⟶ 5,133:
{{libheader|Wren-fmt}}
Wren's Num class already has a static method which does what this task requires.
<syntaxhighlight lang="
System.print("Are these strings numeric?")
|