4

I can't get my mistake in this command:

a=2
b=5
c=3
printf "%.2f\t" "'$a'+'$c'*'$b'" > ofile.txt

I am getting the value as 50.00. But I should get it 17.00.

How to do this when a, b, c are floating values? e.g. a=2.4, b=5.1 and c=3.2

Kay
  • 1,957
  • 2
  • 24
  • 46

2 Answers2

7

Your 2nd argument to printf is interpreted as the string '2', which has an ascii value of 50. If you want to do arithmetic, use arithmetic evaluation in bash:

printf "%.2f\t" "$((a+b*c))" > ofile.txt
Denis
  • 530
  • 2
  • 7
1
printf "%.2f\t" "$(($a+$b*$c))" > yourname.txt
Jahid
  • 21,542
  • 10
  • 90
  • 108
Fiido93
  • 1,918
  • 1
  • 15
  • 22