Arithmetic Operations

Sort by

recency

|

242 Discussions

|

  • + 0 comments

    read -r exp awk "BEGIN { printf \"%.3f\n\", $exp }"

    • BEGIN keyword tells AWK to execute the code without any inputs files or stdin
    • Does a shell expansion of $exp and evaluates the expression to 3 d.p.
    • On a side note the backslash after printf is to escape the double quotes so that awk can treat that as a literal double quote.
  • + 0 comments

    read -r expression

    evaluation={expression}" | bc)

    printf '%.*f\n' 3 "$evaluation"

  • + 0 comments

    read exp

    result=exp" | bc -l)

    printf "%.3f\n" "$result"

  • + 1 comment
    read exp_in
    echo "scale=10; $exp_in" | bc -l | awk '{printf "%.3f\n", $0}'
    
  • + 0 comments

    Just another way using here strings "<<<"

    read expression result=expression") printf "%.3f\n" "$result"