Rounding off a decimal


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Rounding off a decimal
# 1  
Old 01-16-2015
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.
# 2  
Old 01-16-2015
Hello Sanjaykumar28,

Welcome to forums, please use code tags for commands/cods/Inputs used in your posts as per forum rules.
Regarding your query following may help you in same.
Code:
echo "4.41 4.11 4.51" | awk '{printf "%d,%d,%d\n",$1,$2,$3}'

Output will be as follows.
Code:
4,4,4

Thanks,
R. Singh
# 3  
Old 01-16-2015
Thanks for your reply Ravinder. But, I need to round off the decimal to higher interger i.e to 5 and not to 4. Also, I need the result using the ceil command.
# 4  
Old 01-16-2015
Hello Sanjaykumar28,

Following may help you in same. Following is the script named test.ksh.
Code:
cat test.ksh   
float="4.41"
int=${float%.*}
int_val=$((int + 1))
echo $int_val

Output will be as follows.
Code:
5


Thanks,
R. Singh
# 5  
Old 01-16-2015
There is a ceil() function in C, but there is no utility named ceil. But, you can perform the equivalent functionality in a 1993 or later version of ksh or with awk.

Unfortunately, the code suggested by RavinderSingh13 won't work for integral values (even though it works perfectly for non-integral values).

The following should work correctly when given integral or floating point values for values into an integral value of type long integer:
Code:
printf '4.41 4.11 4.51\n4.0000000001 5.99999999, 6\n' | awk '
function roundup(f, i) {
	i = int(f)
	return(i + (i != f))
}
{	printf("%d,%d,%d\n", roundup($1), roundup($2), roundup($3))
}'

which produces the output:
Code:
5,5,5
5,6,6

If you want to try this on a Solaris/SunOS system, change awk to /usr/xpg4/bin/awk, /usr/xpg6/bin/awk, or nawk.

Last edited by Don Cragun; 01-16-2015 at 06:06 AM.. Reason: Add note for Solaris users.
This User Gave Thanks to Don Cragun For This Post:
# 6  
Old 01-16-2015
Using awk.
Code:
#!/bin/bash

list=("$@")

for flt in ${list[*]};
do

echo $flt| awk '{print int($1+0.5)}'
done

# output
# ------
# $ ./rnd.sh 9.51 3.2 7.63 0.6 0.1
#
# 10
# 3
# 8
# 1
# 0

Using /usr/bin/printf
Code:
list=("$@")
precision=2    # can be set to zero for int rounding

for flt in ${list[*]};
do
    echo $flt | LC_ALL=C xargs /usr/bin/printf "%.*f\n" $precision
done
# output
# ------
# $ ./rnd.sh 96.517 33.364 7.63 0.632 0.199
# 96.52
# 33.36
# 7.63
# 0.63
# 0.20


Last edited by ongoto; 01-16-2015 at 09:43 AM..
# 7  
Old 01-16-2015
Quote:
Originally Posted by ongoto
Code:
#!/bin/bash

list=("$@")

for flt in ${list[*]};
do

echo $flt| awk '{print int($1+0.5)}'
done

# output
# ------
# $ ./rnd.sh 9.51 3.2 7.63 0.6 0.1
#
# 10
# 3
# 8
# 1
# 0

Hi ongoto,
If you look at the 1st post in this thread, you'll see that the desired results from the 5 values you used would be:
Code:
10
4
8
1
1

so your proposal produced the desired result on 60% of your samples.
 
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Sum the fields with 6 decimal places - getting only 2 decimal places as output

I used the below script to Sum up a field in a file based on some unique values. But the problem is when it is summing up the units, it is truncating to 2 decimals and not 6 decimals as in the input file (Input file has the units with up to 6 Decimals – Sample data below, when the units in the 2... (4 Replies)
Discussion started by: brlsubbu
4 Replies

2. Programming

Urgent help needed.. C++ program to convert decimal to hexa decimal

Hi , seq can be 0...128 int windex = seq / 8; int bindex = seq % 8; unsigned char bitvalue = '\x01' << (7-bindex) ; bpv.bitmapvalue = bitvalue; This is the part of a program to convert decimal to bitmap value of hexadecimal. I want this to change to convert only to... (1 Reply)
Discussion started by: greenworld123
1 Replies

3. UNIX for Dummies Questions & Answers

Convert hexa decimal to decimal

Hi, I want to convert two hexadecimal numbers to decimal using unix command line. 1cce446295197a9d6352f9f223a9b698 fc8f99ac06e88c4faf669cf366f60d I tried using `echo "ibase=16; $no |bc` printf '%x\n' "1cce446295197a9d6352f9f223a9b698" but it doesn't work for such big number it... (4 Replies)
Discussion started by: sudhakar T
4 Replies

4. 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

5. 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

6. Shell Programming and Scripting

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 (2 Replies)
Discussion started by: jacobs.smith
2 Replies

7. Homework & Coursework Questions

Decimal to BCD (Binary Coded Decimal)

Use and complete the template provided. The entire template must be completed. If you don't, your post may be deleted! 1. The problem statement, all variables and given/known data: Design an algorithm that accepts an input a decimal number and converts it into BCD (Binary... (2 Replies)
Discussion started by: caramba
2 Replies

8. UNIX for Dummies Questions & Answers

Decimal to BCD (Binary Coded Decimal)

Anybody please help me... Design an algorithm that accepts an input a decimal number and converts it into BCD (Binary Coded Decimal) representation. Also, draw its Flow Chart. This is a unix qn... plz post algorithm for that :confused: (1 Reply)
Discussion started by: caramba
1 Replies

9. 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
Login or Register to Ask a Question