PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Sunday, August 7, 2022

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

 August 07, 2022     bash, decimal, multiplication     No comments   

Issue

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


Answered By - M. Nejat Aydin
Answer Checked By - Marilyn (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing