Course of Raku / Advanced / Operators / User-defined operators / Exercises / Percent-of operator
Solution: Percent-of operator
Here is a possible solution to the task.
Code
sub infix:<percent-of>($p, $whole) {
$whole * $p / 100
}
say 20 percent-of 150;🦋 You can find the source code in the file percent-of-operator.raku.
Output
30Comments
The operator takes the percentage
$pas its left operand and the whole amount$wholeas its right operand.Multiplying
$wholeby$pand dividing by100gives the requested part:150 * 20 / 100is30.