Determine if a string is numeric: Difference between revisions

m
(Added solution for Action!)
Line 2,611:
 
=={{header|Julia}}==
The function <tt>isnumericisnumber</tt> tests for strings that parse directly to numbers. This test excludes symbols, such as &pi; and <tt>1 + 1</tt>, 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.
 
<lang julia>using Printf
 
isnumericisnumber(s::AbstractString) = tryparse(Float64, s) isa Number
 
tests = ["1", "-121", "one", "pi", "1 + 1", "NaN", "1234567890123456789", "1234567890123456789123456789",
Line 2,621:
 
for t in tests
fl = isnumericisnumber(t) ? "is" : "is not"
@printf("%35s %s a direct numeric literal.\n", t, fl)
end</lang>
Anonymous user