Re: how to round off a decimal to the next whole number

2008-08-08 Thread Dr.Ruud
[EMAIL PROTECTED] schreef:

 How do I round off a decimal to the next nearest whole digit ,
 example
 0.123 = 1,
 1.23 = 2,
 4.7312 = 5, etc etc.

Define next.

You can use POSIX::ceil(),
but then -1.23 becomes -1 and you might want -2 there?

$ echo -1.23 4.1 5 |perl -MPOSIX -nwle'print ceil($_) for split'
-1
5
5

$ echo -1.23 4.1 5 |perl -MPOSIX -anwle'print $_0 ? floor($_) :
ceil($_) for @F'
-2
5
5

-- 
Affijn, Ruud

Gewoon is een tijger.


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/




RE: how to round off a decimal to the next whole number

2008-08-06 Thread Stewart Anderson

 -Original Message-
 From: Anirban Adhikary [mailto:[EMAIL PROTECTED]
 Sent: 06 August 2008 06:51
 To: beginners@perl.org
 Subject: Re: how to round off a decimal to the next whole number
 
 On Wed, Aug 6, 2008 at 11:01 AM, [EMAIL PROTECTED] wrote:
 
  Hi,
  How do I round off a decimal to the next nearest whole digit ,
  example
  0.123 = 1,
  1.23 = 2,
  4.7312 = 5, etc etc.
 
  Right now I can only do the above by extracting the first digit
using
  splice , then add one.
 
  Thanks
 

This is straight from the Perl FAQ.

sub round {
my($number) = shift;
return int($number + .5);
}

Stu



Information in this email including any attachments may be privileged, 
confidential and is intended exclusively for the addressee. The views expressed 
may not be official policy, but the personal views of the originator. If you 
have received it in error, please notify the sender by return e-mail and delete 
it from your system. You should not reproduce, distribute, store, retransmit, 
use or disclose its contents to anyone. Please note we reserve the right to 
monitor all e-mail communication through our internal and external networks. 
SKY and the SKY marks are trade marks of British Sky Broadcasting Group plc and 
are used under licence. British Sky Broadcasting Limited (Registration No. 
2906991), Sky Interactive Limited (Registration No. 3554332), Sky-In-Home 
Service Limited (Registration No. 2067075) and Sky Subscribers Services Limited 
(Registration No. 2340150) are direct or indirect subsidiaries of British Sky 
Broadcasting Group plc (Registration No. 2247735). All of the companies 
mentioned in this paragraph are incorporated in England and Wales and share the 
same registered office at Grant Way, Isleworth, Middlesex TW7 5QD.

--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/




Re: how to round off a decimal to the next whole number

2008-08-06 Thread Mr. Shawn H. Corey
On Wed, 2008-08-06 at 13:31 +0800, [EMAIL PROTECTED] wrote:
 Hi,
 How do I round off a decimal to the next nearest whole digit ,
 example
 0.123 = 1,
 1.23 = 2,
 4.7312 = 5, etc etc.
 
 Right now I can only do the above by extracting the first digit using splice 
 , then add one.
 
 Thanks
 
 

#!/usr/bin/perl

use POSIX;

my @nbrs = (
  0.123,
  1.23,
  4.7312,
);

for my $n ( @nbrs ){
  print number: $n\n;
  printf \tceil: %d\n, ceil( $n );
  printf \tfloor: %d\n, floor( $n );
  printf \tround: %.0f\n, $n;
}

__END__



-- 
Just my 0.0002 million dollars worth,
  Shawn

Where there's duct tape, there's hope.

Perl is the duct tape of the Internet.
Hassan Schroeder, Sun's first webmaster


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/




Re: how to round off a decimal to the next whole number

2008-08-06 Thread Jeff Pang
[EMAIL PROTECTED] 写道:
 Hi,
 How do I round off a decimal to the next nearest whole digit ,
 example
 0.123 = 1,
 1.23 = 2,
 4.7312 = 5, etc etc.

$number = int($number) + 1;
also does the same thing.

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/




Re: how to round off a decimal to the next whole number

2008-08-06 Thread Rob Dixon
[EMAIL PROTECTED] wrote:

 How do I round off a decimal to the next nearest whole digit ,
 example
 0.123 = 1,
 1.23 = 2,
 4.7312 = 5, etc etc.
 
 Right now I can only do the above by extracting the first digit using splice 
 , then add one.

You need the ceil() function from the POSIX module. All other solutions here
will fail if the fractional part of the number is zero (for instance 5.000 will
be upgraded to 6, which I presume isn't wanted).

use strict;
use warnings;

use POSIX;

foreach (qw(0.123 1.23 4.7312 5.000)) {
  print ceil($_),   ;
}

**OUTPUT**

1  2  5  5


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/




how to round off a decimal to the next whole number

2008-08-05 Thread itshardtogetone

Hi,
How do I round off a decimal to the next nearest whole digit ,
example
0.123 = 1,
1.23 = 2,
4.7312 = 5, etc etc.

Right now I can only do the above by extracting the first digit using splice 
, then add one.


Thanks


--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/




Re: how to round off a decimal to the next whole number

2008-08-05 Thread Anirban Adhikary
On Wed, Aug 6, 2008 at 11:01 AM, [EMAIL PROTECTED] wrote:

 Hi,
 How do I round off a decimal to the next nearest whole digit ,
 example
 0.123 = 1,
 1.23 = 2,
 4.7312 = 5, etc etc.

 Right now I can only do the above by extracting the first digit using
 splice , then add one.

 Thanks


 --
 To unsubscribe, e-mail: [EMAIL PROTECTED]
 For additional commands, e-mail: [EMAIL PROTECTED]
 http://learn.perl.org/


 Just use split function like this

my $val= 0.123;
my($a,$b)=split(/\./,$val);
my $final=($a+1);
print $val=$final.\n;

Thanks  Regards
Anirban Adhikary