Course of Raku / Essentials / Typed variables / Exercises / Converting user input to a number
Solution: Converting user input to a number
The program can be as simple as this:
Code
my Int $n = prompt 'Enter a number: ';
say $n;
🦋 You can find the source code in the file input-number.raku.
Example
Run it and enter an integer number, either positive or negative:
$ raku exercises/typed-variables/input-number.raku
Enter a number: -42
-42
Comments
As we’ve seen, the return value of prompt
is IntStr
, which can be assigned to an Int
variable with no coercion.
Notice, though, that the program will be terminated with an exception if you enter a string that cannot be converted to an integer number.
Course navigation
← Data type conversion / Converting types with prefix operators | Positional data types →
Translations of this page: English • Deutsch • Español • Italiano • Latviešu • Nederlands • Български • Русский • Українська