Find the answer to your Linux question:
Results 1 to 6 of 6
I need to be able to round numbers in bash to the nearest whole number. So for example the number 4.678923 would round up to 5. I tried to use ...
  1. #1
    Just Joined!
    Join Date
    Sep 2005
    Posts
    99

    rounding numbers in bash



    I need to be able to round numbers in bash to the nearest whole number. So for example the number 4.678923 would round up to 5. I tried to use the cut command and delimit by a '.' but that won't work because it will take the whole thing after the decimal point as one big number so the number 2.18745 would get rounded up when it should get rounded down because it takes 1875 as the number and compares it to see if its greater than 5 which it is so then it rounds up. I need a way to just get the first number after the decimal point. I tried the -c command but that takes characters then when i go into the if statement to compare it to 5 it says it needs an integer. Any ideas?

  2. #2
    Linux User
    Join Date
    Aug 2006
    Posts
    458
    if you have Python, here's an alternative:
    Code:
    echo "4.678923" | python -c "print round(float(raw_input()))"

  3. #3
    Just Joined!
    Join Date
    Sep 2005
    Posts
    99
    nope i got it heres what i did, it takes the number to be rounded as a command line argument. Then it cuts delimitted by the decimal point. Then it takes the number right of the decimal because to round you only need the first one so -c1 takes the first character and that gets passed into the if statement to see if its 5 or more to round up.
    Code:
    var=$1
    
    varInt=`echo $var | cut -d '.' -f1`
    varRest=`echo $var | cut -d '.' -f2`
    varFirstPlace=`echo $varRest | cut -c1`
    
    if [ "$varFirstPlace" -gt 4 ]
    then
    echo "Round up"
    (( varInt++ ))
    echo  " integer part after rounding" $varInt
    fi

  4. #4
    Linux User
    Join Date
    Aug 2006
    Posts
    458
    good, now you have to code for round down?
    you can do rounding with awk too... saves you a lot of redundant code
    Code:
    # awk -v var="4.67232" 'BEGIN { rounded = sprintf("%.0f", var); print rounded }'
    5
    # awk -v var="4.13432" 'BEGIN { rounded = sprintf("%.0f", var); print rounded }'
    4
    # awk -v var="4.323" 'BEGIN{ printf"%0.f\n", var}'
    4
    # awk -v var="4.563453" 'BEGIN{ printf"%0.f\n", var}'
    5

  5. #5
    Just Joined!
    Join Date
    Sep 2005
    Posts
    99
    well rounding down the integer part of the number wouldn't change so you dont have to do anything

  6. #6
    Just Joined!
    Join Date
    Nov 2009
    Posts
    2

    yet another solution

    # Use printf to do the rounding.

    for i in $( seq 1 .1 2 ); do printf "%f rounds to %0.f\n" $i $i; done # rounds up to the nearest integer
    1.000000 rounds to 1
    1.100000 rounds to 1
    1.200000 rounds to 1
    1.300000 rounds to 1
    1.400000 rounds to 1
    1.500000 rounds to 2
    1.600000 rounds to 2
    1.700000 rounds to 2
    1.800000 rounds to 2
    1.900000 rounds to 2
    2.000000 rounds to 2

    # So if you had a number like 4.123456 and you want it rounded to a integer
    printf "%0.f\n" 4.123456
    stdout-> 4

    # assign it to some variable
    shinynewnumber=$( printf "%0.f\n" 4.123456 )
    echo $shinynewnumber
    stdout-> 4

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •