Course of Raku / Essentials / Data type conversion / Exercises / True and False numbers

Solution: True and False numbers

To convert a number to a Boolean value, you can use either the ? prefix operator or a constructor form, or call the Bool method, or the so routine as either a prefix operator or a method:

my $value = 42;
say ?$value;
say Bool($value);
say $value.Bool;
say so $value;
say $value.so;

It is quite obvious that non-zero values are converted to True. We are thus most interested in looking at those options that become False.

All zeroes, integer, floating-point, or rational, are cast to False:

my Int $int-zero = 0;
say ?$int-zero; # False

my Rat $rat-zero = 0.0;
say ?$rat-zero; # False

my Num $num-zero = 0e0;
say ?$num-zero; # False

Of course, nothing changes if you try to negate the number first:

my $int = 0;
say ?(-$int); # False

🦋 You can find the full program with the above example in the file true-false-numbers.raku.

Next exercise

💪 True and False strings

Course navigation

Loops / Postfix form of for   |   Typed variables