Re: Should to_number return a sequence?
- Posted by irv 5 days ago
- 347 views
How about returning the original string when the conversion fails?
include std/get.e include std/types.e include std/console.e function val (object s) if atom(s) then return s end if for i = 1 to length(s) do if s[i] > '9' then return s end if end for sequence v = value(s) if v[1] = GET_SUCCESS then return v[2] else return s end if end function object a object tests = {"3.14","-1","+2.34",42,"99hello","Hi 44"} for i = 1 to length(tests) do a = val(tests[i]) if string(a) then display("[] failed!",{a}) -- show a warning or something else display("[] => [] success",{tests[i],a}) -- use it. end if end for
irv@irv-desktop:~$ eui test 3.14 => 3.14 success -1 => -1 success +2.34 => 2.34 success 42 => 42 success 99hello failed! Hi 44 failed!