Go Back   The UNIX and Linux Forums > Top Forums > UNIX for Dummies Questions & Answers
Search Forums:



UNIX for Dummies Questions & Answers If you're not sure where to post a UNIX or Linux question, post it here. All UNIX and Linux newbies welcome !!

Reply    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 02-07-2012
Registered User
 

Join Date: Apr 2011
Posts: 39
Thanks: 0
Thanked 0 Times in 0 Posts
Rounding up to nearest whole number

Hi all of you,

Would be great if you help me with how to round up to whole number from my input values like
2.99996,2.17890,3.00002,-2.3456,-2.7890
o/p should be like 3,2,3,-2,-3

thnks in adv!!!!
regards
Sponsored Links
    #2  
Old 02-08-2012
balajesuri's Avatar
#! /bin/bash
 

Join Date: Apr 2009
Location: India
Posts: 1,021
Thanks: 9
Thanked 285 Times in 277 Posts

Code:
#! /usr/bin/perl -w
use strict;

my @x = (2.99996,2.17890,3.00002,-2.3456,-2.7890);

($_ > 0) ? ($_ - int($_) < 0.5) ? print int($_), "\n" : print int($_)+1, "\n" : ($_ - int($_) > -0.5) ? print int($_), "\n" : print int($_)-1, "\n" for (@x);

#Un-comment the below two lines and comment the above line if you want output as comma separated values
#($_ > 0) ? ($_ - int($_) < 0.5) ? push (@y, int($_)) : push (@y, int($_)+1) : ($_ - int($_) > -0.5) ? push (@y, int($_)) : push (@y, int($_)-1) for (@x);
#print join (",", @y);


Last edited by balajesuri; 02-08-2012 at 12:33 AM..
Sponsored Links
    #3  
Old 02-08-2012
Registered User
 

Join Date: Apr 2011
Posts: 39
Thanks: 0
Thanked 0 Times in 0 Posts
many many thanks ,it worked!!!!!
    #4  
Old 02-08-2012
Scrutinizer's Avatar
mother ate her
 

Join Date: Nov 2008
Location: Amsterdam
Posts: 5,382
Thanks: 82
Thanked 1,115 Times in 1,019 Posts
In shell:

Code:
$ printf "%1.f\n" 2.99996
3

Sponsored Links
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Rounding number, but.... Gery Shell Programming and Scripting 9 04-28-2011 09:40 AM
AWK Match to nearest number ian_gooch Shell Programming and Scripting 7 04-13-2011 06:17 AM
PERL - rounding fractional number ganapati Shell Programming and Scripting 8 11-28-2010 11:27 AM
Rounding off decimals to the nearest number in PERL rdlover Shell Programming and Scripting 9 03-30-2009 12:56 AM
Rounding off to the next whole number damansingh Shell Programming and Scripting 2 07-17-2008 06:18 AM



All times are GMT -4. The time now is 03:42 AM.