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.

Code

The possible solution is shown below:

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

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

Output

$ raku exercises/coercion/fractional-part.raku
0.8972

Comment

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
-0.8972

Course navigation

Loops / Postfix form of for   |   Typed variables

Translations of this page: EnglishDeutschEspañolItalianoLatviešuNederlandsБългарскиРусскийУкраїнська