Home > Software design >  how to compute the float nuber in Linux?
how to compute the float nuber in Linux?

Time:05-09

I expect to compute the answer of the expression a=0;b=0.055;echo $((a * 60 b))

however the output is as follows:

syntax error: invalid arithmetic operator (error token is ".055")

I expect the output is 0.055

when I input : a=0;b=0.055;echo "scale=3;a * 60 b"|bc -l

the output is : 0

and input:a=0;b=0.055;echo "scale=3;a * 60 b"|bc

the output is : 0

how can I solve this problem?

CodePudding user response:

You were pretty close. Use $a and $b instead of just a and b like this:

a=0;b=0.055;echo "scale=3; (($a * 60   $b)*100)/100"|bc

or in a file like this:

test.sh

#!/bin/bash

a=0
b=0.055
result=$(echo "scale=3; (($a * 60   $b)*100)/100" | bc)
echo $result

That should give you the desired result of 0.055.

See https://askubuntu.com/questions/217570/bc-set-number-of-digits-after-decimal-point answer regarding how to handle scale with bc

  • Related