Arithmetic Operations

Sort by

recency

|

245 Discussions

|

  • + 0 comments
    read x
    
    awk "BEGIN { printf \"%.3f\n\", $x }"
    
  • + 0 comments

    for fun :)

    read expression
    
    result_l1=$( bc <<< "scale=4; ( $expression )" )
    result_l2=${result_l1::-2}$((${result_l1: -2:2}+5))
    
    echo ${result_l2::-1}
    
  • + 0 comments

    read -p "Enter expression: " expr result=expr" | bc -l) printf "%.3f\n" "$result"

  • + 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"