Skip to content

Instantly share code, notes, and snippets.

@RayMPerry
Created February 9, 2021 01:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save RayMPerry/61f7a2efb5f864dc9ad8853ce147e984 to your computer and use it in GitHub Desktop.
Save RayMPerry/61f7a2efb5f864dc9ad8853ce147e984 to your computer and use it in GitHub Desktop.
Tip Calculator
use v6;
subset DollarAmount of Cool where * ~~ ^<[0 .. 9\.]>+$;
subset Percentage of DollarAmount where * > 1;
my $bill-amount;
my $tax-percentage;
repeat {
$bill-amount = prompt "What is the bill amount? "
} until DollarAmount($bill-amount) !~~ Failure;
repeat {
$tax-percentage = prompt "What is the tip percentage? "
} until Percentage($tax-percentage) !~~ Failure;
my $tip-amount = ($bill-amount * ($tax-percentage / 100)).round: 0.01;
say "The tip amount is \$$tip-amount.";
say "The total amount is \$", $bill-amount + $tip-amount, ".";
@salortiz
Copy link

salortiz commented Feb 9, 2021

This works:

subset DollarAmount of Cool where /^ <[0 .. 9\.]>+ $/;
subset Percentage of Numeric where * > 1;

my $bill-amount; 
my $tax-percentage; 

repeat {
    $bill-amount = prompt "What is the bill amount? "
} until $bill-amount ~~ DollarAmount;

repeat {
    $tax-percentage = prompt "What is the tip percentage? "
} until $tax-percentage ~~ Percentage;

my $tip-amount = ($bill-amount * ($tax-percentage / 100)).round: 0.01;

say "The tip amount is \$$tip-amount.";
say "The total amount is \$", $bill-amount + $tip-amount, ".";

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment