Sort 218 Discussions, By:
Please Login in order to post a comment
read -p "Enter the expression: " exp
printf "%.3f" $(echo $exp | bc -l)
where 1st line will take the expression, and
2nd line will read the expression using echo $exp, perform the calculations using bc -l and print the result upto 3 decimal places using printf "%.3f"
var=$(echo "scale=4; $x" | bc)
printf "%.3f\n" "$var"
The simplest solution:
printf "%0.3f" $(cat - | bc -l)
cat - reads a numeric value from standard input (stdin).
| pipes the input to the next command.
bc -l performs arbitrary-precision calculations, including math functions like sine and cosine.
$(...) captures the result of the bc calculation.
printf "%0.3f" formats and prints the captured result as a floating-point number with three decimal places.
In summary, this code reads a number, performs a mathematical operation, and displays the result with three decimal places.
new_number1=$( echo "scale=4; $number " | bc)
new_number2=$( echo "scale=3; $number " | bc)
if(( last1 < 5)); then
if(( last2 < 9 )); then
result= expression" |bc )