Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to output float result?

I am working on bash script: need to evaluate an input and then output the result
as a float *.xxx:
I do:

read var1
var2=$((var1))
#echo $var2
echo $((var1))

an input: 5+503/20 + (192)/7
my output is 17 but it should 17.929

How to evaluate it as a float?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

Bash only supports integer arithmetic.

In your case, you can use bc(1).

read var1
var2="$(bc <<<"scale=2;$var1")"

Use the scale variable in bc(1) to set the number of significant digits (default with is 0).

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading