You can use accept
to grab user input and then s>number?
to attempt to convert it to a number. accept
expects a memory address and a max length. 's>number?` leaves a double and a flag.
So you could do something like
: fetch-input ( - n f )
pad 40 accept
pad swap s>number?
>r d>s r> ;
If s>number?
can't convert the string to a number it will leave 0 0
under the flag on the stack.
So after s>number?
you have three entries on the stack: the two parts of the double precision fixed point number and a flag--stack comment would be: ( - n n f ). But I'm assuming you want a single precision number given the code in your question.
d>s
will take a double precision number and convert it to single precision. We need to move the flag out of the way so we can get to the number. A standard trick for temporarily moving something out of the way is to move it from the data stack to the return stack, do whatever it is you need to do, then move it back. Thus, >r
and r>
.