Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

Round up 3 values to the next 100

by siva kumar (Pilgrim)
on Feb 14, 2007 at 06:32 UTC ( [id://599863]=perlquestion: print w/replies, xml ) Need Help??

siva kumar has asked for the wisdom of the Perl Monks concerning the following question:

I have a small task that rounds up last 3 values to the next 100 in a integer.
Example:
78345 shoud become 78400 AND 5674 should become 5700
I completed the task using substr. I think the same task can be easily finished by a regex. Please help.
My code is here ..
$num = 786733; $addFlag = 0; if(length($num) >= 3 ) { $var = substr($num,-3,1); $var += 1; if($var1 == 10) { $addFlag = 1; $var1 = 0; } $var .='00'; $restN = length($num) - 3; $var1 = substr($num,0,$restN); $var1 +=1 if $addFlag; print $var1.$var; } output: 786800;

Replies are listed 'Best First'.
Re: Round up 3 values to the next 100
by bobf (Monsignor) on Feb 14, 2007 at 06:45 UTC
      I hope ceil/floor is used for decimal numbers conversion. But I have only integers that last 3 digits rounded to next 100.

        use strict; use warnings; use POSIX qw( ceil ); my $round_to = 100; for( 78345, 5674 ) { print "$_ -> ", ceil( $_/$round_to ) * $round_to, "\n"; }
        Output:
        78345 -> 78400 5674 -> 5700

        Take a look at the thread I mentioned in my previous post. There are quite a few examples to help you.

        I might have done this
        $num = 235634; if (length($num) >=3 ) { $var = substr($num,-2,2); if ( !$var eq '00') { $var1 = 100 - $var; $num += $var1; } print $num; }
        I guess this will work....
        $a is the number to be rounded
        #!/usr/bin/perl -w $a=78345; $b=100; $c=$a/$b; $d=$c; $c=~s/\..*//; if( $d > $c ){ $c++; } $c*=100; print $c,"\n";
        $c will have the result
Re: Round up 3 values to the next 100
by imp (Priest) on Feb 14, 2007 at 06:39 UTC
    What's wrong with traditional rounding?
    my $c = 786733; my $rounded = 100 * int(($c+ 50) / 100);
    Update
    If you are looking to always skip to the next 100 then change the 50 to 100. If you want to round up except if it is already divisible by 100, then add 99. For example:
    my $c = 786001; print 100 * int(($c + 99) / 100);
Re: Round up 3 values to the next 100
by johngg (Canon) on Feb 14, 2007 at 11:01 UTC
    Expanding on bobf's solution, I wrote a module a while ago that uses POSIX::ceil(), floor() and pow() functions that allows me to round to a specified number of decimal places including negative values to round to the nearest 10, 100 etc. Here's the module

    # Rounders.pm # # ======== package Rounders; # ======== use strict; use warnings; use Exporter; our @ISA = qw{Exporter}; our @EXPORT = qw{rndZero rndPlaces}; use POSIX qw(ceil floor pow); # ------- sub rndZero # ------- { my $val = shift; my $rounded = $val < 0 ? POSIX::ceil($val - 0.5) : POSIX::floor($val + 0.5); return $rounded; } # --------- sub rndPlaces # --------- { my($val, $places) = @_; my $rounded = rndZero($val * POSIX::pow(10.0, $places)) / POSIX::pow(10.0, $places); return $rounded; } 1;

    a test script to demonstrate usage

    #!/usr/local/bin/perl # use strict; use warnings; use Rounders; my $val = 724934.817084; print qq{Original value - $val\n}; print q{Rounded to whole number - }, rndZero($val), qq{\n}, qq{Rounded to decimal places\n}; printf qq{ %2d - %s\n}, $_, rndPlaces($val, $_) for -4 .. 4;

    and the results

    Original value - 724934.817084 Rounded to whole number - 724935 Rounded to decimal places -4 - 720000 -3 - 725000 -2 - 724900 -1 - 724930 0 - 724935 1 - 724934.8 2 - 724934.82 3 - 724934.817 4 - 724934.8171

    I hope this is of use.

    Cheers,

    JohnGG

Re: Round up 3 values to the next 100
by Mandrake (Chaplain) on Feb 14, 2007 at 08:33 UTC
    Are you very specific about solving this with regex?
    Instead of using substring or regex, I feel this approach is more logical.
    #!/usr/bin/perl -w use strict; my $num = 786603; my $rem = $num % 100; $num += (100 - $rem) if ($rem > 0); print $num."\n";
      Thanks - I'm using this in my code!

        Hi, how about this one?
        This is much more flexible by changing the value of $digit and simple in a one line.

        function roundup($number, $digit = 100) { return ( is_numeric($number) && is_numeric($ digit) ) ? (ceil($numbe +r/$ digit)*$digit) : false; }

        Please feel free to contact me at chae@azstrada.com if you have any questions.
        Chae from Seoul Korea.

Re: Round up 3 values to the next 100
by TOD (Friar) on Feb 14, 2007 at 08:39 UTC
    you should always consider using sprintf() for rounding, e.g.:
    $num = 486733; $num =~ /(\d{3})$/; my $rnd = $1 / 100; $rnd = sprintf "%.0f", $rnd; $rnd .= '00'; $num =~ s/$1/$rnd/;
Re: Round up 3 values to the next 100
by 5mi11er (Deacon) on Feb 14, 2007 at 17:24 UTC

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://599863]
Approved by imp
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others sharing their wisdom with the Monastery: (4)
As of 2024-04-16 14:33 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found