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

bash: integer * float multiplication

I am trying to write a simple multiplication between int and float numbers in bash:

int='10'
float='0.001'

# should give 0.01
multip=$(( float*int ))

produces error

bash: 0.001 : erreur de syntaxe : opérateur arithmétique non valable (le symbole erroné est « .001 »)

what should be fixed ?

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 doesn’t support floats. Use an external program for that, e.g. bc.

int=10
float=0.001
multip=$(bc <<< "$int * $float")

Note that bc doesn’t output the leading 0 in 0.01, so you need to add some string processing, e.g.

multip=${multip/#./0.} # Insert 0 before the leading dot.
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