Jump to content

Determine if a string is numeric: Difference between revisions

(→‎{{header|Haskell}}: ++ gnu octave)
Line 251:
s" beef" is-numeric \ beef as integer = BEEF
s" &1234" is-numeric \ &1234 as integer = 4D2 ( decimal literal )
 
=={{header|GNU Octave}}==
 
The builtin function <tt>isnumeric</tt> return true (1) if the argument is a data of type ''number''; the provided function <tt>isnum</tt> 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.
 
<lang octave>function r = isnum(a)
if ( isnumeric(a) )
r = 1;
else
o = str2num(a);
r = !isempty(o);
endif
endfunction
 
% tests
disp(isnum(123)) % 1
disp(isnum("123")) % 1
disp(isnum("foo123")) % 0
disp(isnum("123bar")) % 0
disp(isnum("3.1415")) % 1</lang>
 
 
=={{header|Haskell}}==
Cookies help us deliver our services. By using our services, you agree to our use of cookies.