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 ...
- 04-13-2007 #1Just 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?
- 04-14-2007 #2Linux 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()))"
- 04-14-2007 #3Just 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
- 04-14-2007 #4Linux 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
- 04-14-2007 #5Just 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
- 11-15-2009 #6Just 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



