Re: Text to Number

new topic     » topic index » view thread      » older message » newer message

> How does one convert a text sequence to a number in Euphoria?
>
> In working with EX07.EXW in the Win32 examples, text entered by the
> user in the dialog box Sle1 is retrieved using the function 
> getText(). I
> assume this returns a sequence of ASCII characters. If I want 
> to confine the user's input to a numeric range from 0 to n, 
> how do I do this?
> In BASIC we would use a = VAL(x$).

If you don't care what the user types in then you can just use getNumber().

    atom n
    n = getNumber(Sle1)

If whatever the user typed in can be converted to a number, this function
will return that value otherwise it returns zero.

If you only want the user to enter digits 0 thru 9, do this:

    Sle1 = create(EditText, "", win, x, y, w, h, ES_NUMBER)
    n = getNumber(Sle1)

If you wish to allow them to enter decimal points you need to resort to
keystroke trapping.

   procedure AllowNumbers(integer keycode, integer shifts)
      if shifts != 0 or find(keycode, "0123456789.") = 0 then
          returnValue(-1) -- Tell Windows to ignore it.
      end if
   end prodedure
   onKeyPress[Sle1] = routine_id("AllowNumbers")

you still need to use getNumber() to convert the text typed in to a number.

--------
Derek.

new topic     » topic index » view thread      » older message » newer message

Search



Quick Links

User menu

Not signed in.

Misc Menu