I have a string that represent a float:
echo $NUM
5.03
I need to multiply this number for MEGA. If I do it directly:
MEGA="1000"
result=$(($NUM*$MEGA))
I receive an error:
syntax error: invalid arithmetic operator (error token is ".03 * 1000")
Bash only has integers, no floats. You'll need a tool like bc
to properly assign the value of result
:
result=$(bc -l <<<"${NUM}*${MEGA}")
Or you can use awk
:
result=$(awk '{print $1*$2}' <<<"${NUM} ${MEGA}")
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With