17

I read price from user input. When i multiply the input with int like this

T="$((PRICE*QTY))"|bc; gives line 272: 12.00: syntax error: invalid arithmetic operator (error token is ".00") or .50

depending on user input. How do i multiply these two variables and get a total with 2 decimal points?

2
  • WOW! This is good! I've never seen replies this quick in any other forums no matter how active they are and mostly get "we wont do your assignment for you" replies. This worked for me T=echo $QTYS\* $PRICE | bc; Its also rounding price to 2 decimal places without scale. For some reason just price*QTY was giving me command not found error. Commented Jul 19, 2010 at 11:06
  • One thing to remember is that bash doesn't truly have any data types except strings. Every variable comes down to a string, so it may help you to remember that. Different programs of course will treat certain strings in a special way. Commented Jul 19, 2010 at 12:58

5 Answers 5

24

this works:

 PRICE=1.1 QTY=21 RES=$(echo "scale=4; $PRICE*$QTY" | bc) echo $RES 
Sign up to request clarification or add additional context in comments.

2 Comments

Could you also explain the use of scale here? What does scale=4 mean.
This is what I was missing when using bc! scale sets the precision of the operation - see the "Variables" section of this page
13
var=$(echo "scale=2;$PRICE*$QTY" |bc) 

You can also use awk

awk -vp=$PRICE -vq=$QTY 'BEGIN{printf "%.2f" ,p * q}' 

5 Comments

./menu3.sh: line 278: 12.25: syntax error: invalid arithmetic operator (error token is ".25")
remove the $(()) . bash doesn't do floating arithmetic. If you want to set 2 decimal places, use scale=2
You need dollar signs before each variable name.
var=$(echo "scale=2;$PRICE*$QTY" |bc) gives me (standard_in) 2: syntax error
bc is not installed on all distributions. I upvoted this one because awk is and he supplied an awk version that is more portable to *nix systems!
4
T="$(echo "$PRICE*$QTY" | bc)" 

2 Comments

i get (standard_in) 2: syntax error idk why. its almost the same as above
@svenus: This one works for me. I don't know why you're getting that error from bc.
2

You can use mul=0.8 exp=200 texp=awk -vp=$mul -vq=$exp 'BEGIN{printf "%.2f" ,p * q}'

Hope this is going to work.

1 Comment

Please edit your answer. Its right, but not properly formatted
1

First, trying to do floating-point arithmetic with bc(1) without using the -l flag is bound to give you some funny answers:

sarnold@haig:~$ bc -q 3.5 * 3.5 12.2 sarnold@haig:~$ bc -q -l 3.5 * 3.5 12.25 

Second, the $((...)) is an attempt to do arithmetic in your shell; neither my bash nor dash can handle floating point numbers.

If you want to do the arithmetic in your shell, note printf(1) as well as (probably) your shell's built-in printf function. If you want to do the arithmetic in bc, note the special variable scale.

1 Comment

most of the $(( )) with bc i tried gave me errors. Must be because of the (( ))? Thanks for the clarification.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.