Welcome to Linux Forums! With a comprehensive Linux Forum, information on various types of Linux software and many Linux Reviews articles, we have all the knowledge you need a click away, or accessible via our knowledgeable members.
Find the answer to your Linux question:
New to Linux Forums? Register here for free!
    Linux Forums > GNU Linux Zone > Linux Programming & Scripting > rounding numbers in bash

Forgot Password?
 Linux Programming & Scripting   C, Perl, PHP, Bash Scripts, anything programming or script related post in here!

Site Navigation
Linux Articles
Linux Forums
Linux Downloads
Linux Hosting
Free Magazines
Job Board
IRC Chat
RSS Feeds


Linux Forum Topics
Linux Forums
Your Distro
Linux Resources
GNU Linux Zone
The Community
Closed Thread
 
Thread Tools Display Modes
Old 04-13-2007   #1 (permalink)
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?
cwl157 is offline  


Old 04-14-2007   #2 (permalink)
Linux User
 
Join Date: Aug 2006
Posts: 434
if you have Python, here's an alternative:
Code:
echo "4.678923" | python -c "print round(float(raw_input()))"
ghostdog74 is offline  
Old 04-14-2007   #3 (permalink)
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
cwl157 is offline  
Old 04-14-2007   #4 (permalink)
Linux User
 
Join Date: Aug 2006
Posts: 434
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
ghostdog74 is offline  
Old 04-14-2007   #5 (permalink)
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
cwl157 is offline  
Old 6 Days Ago   #6 (permalink)
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
brianarb is offline  
Closed Thread


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off

Free Magazines
Run Your Own Web Server Using Linux & Apache - Free 191 Page Preview
Learn about everything you'll need to build and maintain your Linux servers, and to deploy Web applications to them.
subscribe
Open Source Security Myths Dispelled
Dispel the five major myths surrounding Open Source Security and gain the tools necessary to make a truly informed decision for your IT organization
subscribe
InformationWeek
InformationWeek is the only newsweekly you'll need to stay on top of the latest developments in information technology.
subscribe



All times are GMT. The time now is 07:51 AM.






© 2000 - 2009 - All Rights Reserved - Property of  MAS Media

Content Relevant URLs by vBSEO 3.3.0 RC2