Visit Our UNIX and Linux User Community

Rounding off decimal values

Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Rounding off decimal values
# 1  
Old 02-19-2013
Rounding off decimal values

Hi Friends,

This is my last post for today.

My input file is

chr1 100 200
chr1 123 300
chr1 300 400 
chr1 420 520
chr10 132344343 132348674

When I try using this command

awk '{v=($3+$2)/2; print $0"\t"v}' 1

This is my output

chr1 100 200	150
chr1 123 300	211.5
chr1 300 400 	350
chr1 420 520	470
chr10 132344343 132348674	1.32347e+08

But, I want to round off the 0.5s to nearest whole number and I dont want the exponential notation. Instead, I would like to see the real number

So, my final output would be

chr1 100 200	150
chr1 123 300	212
chr1 300 400 	350
chr1 420 520	470
chr10 132344343 132348674	132346509 (This one is rounded of from the exponential notation which is 132346508.5)

# 2  
Old 02-19-2013
echo '123.798' | awk '{printf "%.0f\n",$0}'

This User Gave Thanks to vgersh99 For This Post:
# 3  
Old 02-19-2013
awk 'BEGIN{CONVFMT="%.0f"}{v=($3+$2)/2; print $0"\t"v}'  file

This User Gave Thanks to Yoda For This Post:

Previous Thread | Next Thread
Test Your Knowledge in Computers #866
Difficulty: Easy
Lisp and Fortran are the two oldest high-level programming language still in widespread use today.
True or False?

9 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Rounding off a decimal

How to round off a decimal number to higher whole number using ceil command in unix? Eg. 4.41 or 4.11 or 4.51 should be rounded off to 5. (11 Replies)
Discussion started by: SanjayKumar28
11 Replies

2. Shell Programming and Scripting

Rounding decimal values in a column

Hi, I wanted to round all the values in a column to nearest integer. I have multiple files with only two columns and I want to round values in column 2. e.g input_file A1 23.971578 A2 34.624976 A3 46.403446 A4 375 A5 1 A6 3 A7 ... (3 Replies)
Discussion started by: ashu0001
3 Replies

3. Shell Programming and Scripting

Bash Rounding to 2 decimal places

I have a number in a bash variable n, and want to round it to 2 decimal places. How can I do that? n=0.0867268 Need to have num=0.09 (1 Reply)
Discussion started by: kristinu
1 Replies

4. Shell Programming and Scripting

Working With Values After Decimal

I have two files which have to be compared. One of them has leading & trailing zeroes in certain fields. file1 ---- John,Rambo,20100101,2119.5,3302.39,100.07,22211.0 file2 ---- John,Rambo,20100101,000002119.50,0003302.39,00000.07,000022211.00 I am thinking of using diff to... (10 Replies)
Discussion started by: Sheel
10 Replies

5. Shell Programming and Scripting

How to get decimal values ?

Hi All, In my script I've written like this- c=$( expr 100 / 3);echo $c The output coming is 33. but I want to see 33.33, decimal values too. How to get that? Thanks, Naresh (3 Replies)
Discussion started by: NARESH1302
3 Replies

6. Shell Programming and Scripting

How to sum up two decimal values?

I am running the following script : cat ind_sls_extr_UX.out_sorted | while read each_rec do count=`echo "${each_rec}" | cut -c1-2` if then final_amount=0 amount=`echo "${each_rec}" | cut -c280-287` echo "${amount}" final_amount=`expr ${amount} + ${amount}` ... (7 Replies)
Discussion started by: mady135
7 Replies

7. UNIX for Dummies Questions & Answers

Rounding a decimal

Hi, I am currently using tcsh I am trying to round a decimal number to the ten-thousandths place For instance: 1.23456 is rounded up towards 1.2346 I am not looking for truncation, but for rounding. Anyone know how to do this with awk or expr? Thanks (2 Replies)
Discussion started by: miniwheats
2 Replies

8. Shell Programming and Scripting

Evaluating Decimal values

How can I evaluate a decimal value in an if statement? echo "Enter limit:" read limit (enter a decmal value, ie: 2.5) decimallimit=`echo $limit+0|bc|quit` echo $decimallimit if then echo $decimallimit else echo "failed" fi (4 Replies)
Discussion started by: larrys721
4 Replies

9. UNIX for Advanced & Expert Users

Converting Binary decimal coded values to Ascii Values

Hi All, Is there any command which can convert binary decimal coded values to ascii values... i have bcd values like below оооооооооооо0о-- -v - Pls suggest a way to convert this. Thanks, Deepti.Gaur (3 Replies)
Discussion started by: gaur.deepti
3 Replies

Featured Tech Videos