Arithmetic Operations

Sort by

recency

|

239 Discussions

|

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

  • + 0 comments
    #!/bin/bash
    
    read expression
    
    result=$(echo "scale=4; $expression" | bc -l)
    
    
    if (( $(echo "$result >= 0" | bc -l) )); then
        rounded_result=$(echo "$result + 0.0005" | bc -l)
    else
        rounded_result=$(echo "$result - 0.0005" | bc -l)
    fi
    
    
    final_result=$(echo "scale=3; $rounded_result / 1" | bc -l) # dividing the rounded_result by 1 to ommit the zero at end (if there's any)
    
    
    
    echo $final_result
    
    
        
    
  • + 0 comments

    SOLVED

    start of script

    !/bin/bash

    read expression result=expression" | bc -l) rounded_res=result") echo $rounded_res

  • + 0 comments

    read expr

    result=expr" | bc -l)

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