-1

How can I multiply by decimal numbers and get the result in decimal?? How can the code be modified to meet these two conditions? enter image description here

  • Please read `man bc`. – Jos Oct 24 '22 at 11:09
  • 2
    Please, don't use screenshots of code. Instead, paste your code into your question and format it as code by selecting it and clicking the `{}` icon (or the Ctrl+K shortcut). – Dan Oct 24 '22 at 11:28
  • Related: [Bash only gives integer as output regardless of input when performing calculations](https://askubuntu.com/questions/1108328/bash-only-gives-integer-as-output-regardless-of-input-when-performing-calculatio) – steeldriver Oct 24 '22 at 11:55

1 Answers1

0

You could call the desk-calculator program to do the arithmetic:

v1 = 3
pi = 3.14
$ echo "Area of Circle =" $(dc --expression="$pi $v1 * $v1 * p")
Area of Circle = 28.26

In case you aren't familiar with it, dc uses reverse-polish syntax:

  • stack $pi
  • stack $v1
  • replace the top two numbers with their product
  • stack $v1
  • replace the top two numbers with their product
  • print the top of the stack

These are equivalent expressions:

$ echo "Area of Circle =" $(dc --expression="$pi $v1 * $v1 * p")
Area of Circle = 28.26

$ echo "Area of Circle =" $(dc --expression="$pi $v1 2 ^ * p")
Area of Circle = 28.26

$ echo "Area of Circle =" $(dc --expression="$v1 2 ^ $pi * p")
Area of Circle = 28.26
Ray Butterworth
  • 1,351
  • 2
  • 11
  • 29
  • when use this command>>> linux write: dc stack empty & pi: not found command – Amany ahmad Nov 02 '22 at 06:04
  • @Amanyahmad, did you first set the values of *pi* and *v1*? Before doing the `echo …` line, try `echo $pi` and `echo $v1` to verify that they are set correctly. ¶ Also, make sure you are using the bash shell: `echo $SHELL` should print "/bin/bash". – Ray Butterworth Nov 02 '22 at 13:09