0

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.

wjandrea
  • 28,235
  • 9
  • 60
  • 81
Megan
  • 5
  • 1

2 Answers2

2
for i in 0.99 0.98
do
    printf '%s %s %s\n' $i $(bc <<< "$i * 0.002; $i * 10.234")
done
M. Nejat Aydin
  • 9,597
  • 1
  • 7
  • 17
0

here's a version using awk , NB: the precision is greater, hope this is helpful

echo '0.99 0.98' | awk '{for(X=1;X<= NF;X++){ print $X " " $X * 0.002 " " $X * 10.234 }}'

0.99 0.00198 10.1317
0.98 0.00196 10.0293