Syntax error when trying to add & ldquo; $ & Rdquo; In front of the numbers

advertisements

Im trying to add a "$" in front of the numbers that are calculated below; the only successful one that was able to add the "$" was the Limit since it doesn't have any mathematical conflicts with the total, etc. How would I add the dollar sign in Cost, Tip, Total, Average. I tried to add it like i did with the limit but it gave me a syntax error. Please help me figure this out!

Percent=20
function calc {
  echo -n "What is the total cost in cents?";
  read cents
  guests=5
  Limit='$'$(awk "BEGIN{printf \"%.2f\n\", guests * 10  "); #added successfully
  Cost=$(echo "scale =2; $cents/100" | bc); #need to add $
  Percent=$(echo "scale =2; $Percent / 100" | bc);
  Tip=$(echo "scale =2; $Cost * $Percent" | bc); #need to add $
  Total=$(echo "scale =2; $Cost + $Tip" | bc); #need to add $
  Average=$(echo "scale =2; $Total/guests" | bc); #need to add $
}

I have a code similar posted but it isn't the same since the one before hand isn't helping me with the "$" issue that i'm having now


You cannot add the $ to variables while you still use the variables to calculate something. Its working for Limit because Limit isn't used in the following calculations. Execute the program "by hand" and see what happens:

What happens:

Cost='$12.00' # value computed by hand
Tip='$1.00'   # value computed by hand
Total=$(echo 'scale=2; $12.00 + $1.00' | bc); # filled in values of variables

bc doesn't know how to handle the $. Complete all calculations without the $ and then (at the very end) add the $.

# ongoing calculations
Cost='$'"$Cost"
Tip='$'"$Tip"
Total='$'"$Total"
Average='$'"$Average"