|
Search Forums:
|
|||||||
| Forums | Register | Forum Rules | Linux and Unix Links | Man Pages | Albums | FAQ | Users | Calendar | Search | Today's Posts | Mark Forums Read |
| 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 !! |
|
|
|
Thread Tools | Search this Thread | Display Modes |
|
#1
|
|||
|
|||
|
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
|
||||
|
||||
|
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
|
|||
|
|||
|
many many thanks ,it worked!!!!!
|
|
#4
|
||||
|
||||
|
In shell: Code:
$ printf "%1.f\n" 2.99996 3 |
| Sponsored Links | ||
|
|
![]() |
| Thread Tools | Search this Thread |
| 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 |
|
|