Search code examples
bashdecimalmultiplication

How do you perform decimal multiplications and print the responses out on one line in Bash?


I would like to perform the following series of multiplications and print the answers out on two lines as shown below.

for i in 0.99 0.98
do
    echo -n "$i"
    echo "$i * 0.002" | bc
    echo "$i * 10.234" | bc
done

It currently prints out in four lines:

0.99 .001 
10.131
0.98 .001
10.029

Instead I need it to print out in two lines:

0.99 .001 10.131
0.98 .001 10.029

I am willing to make any necessary changes to the code as long as I obtain the answer in two lines as shown above.


Solution

  • for i in 0.99 0.98
    do
        printf '%s %s %s\n' $i $(bc <<< "$i * 0.002; $i * 10.234")
    done