Course of Raku / Essentials / Data type conversion / Exercises / Fractional part

Solution: Fractional part

To get the fractional part of a number, you can subtract the integer part, which you can get by casting the number to an Int.


The possible solution is shown below:

my $n = 15.8972;
say $n - $n.Int;

🦋 Find the program in the file fractional-part.raku.


$ raku exercises/coercion/fractional-part.raku


Try the same program with negative numbers too, for example:

my $n = -15.8972;
say $n - $n.Int;

In this case, the result should also be correct:

$ raku exercises/coercion/fractional-part.raku

Course navigation

Loops / Postfix form of for   |   Typed variables