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?
You can also use awk
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.
this works:
First, trying to do floating-point arithmetic with
bc(1)
without using the-l
flag is bound to give you some funny answers:Second, the
$((...))
is an attempt to do arithmetic in your shell; neither mybash
nordash
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-inprintf
function. If you want to do the arithmetic in bc, note the special variablescale
.