in reply to Re: Round up 3 values to the next 100
in thread Round up 3 values to the next 100

I hope ceil/floor is used for decimal numbers conversion. But I have only integers that last 3 digits rounded to next 100.
  • Comment on Re^2: Round up 3 values to the next 100

Replies are listed 'Best First'.
Re^3: Round up 3 values to the next 100
by bobf (Monsignor) on Feb 14, 2007 at 07:07 UTC

    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.

      Thank you 'bobf'. Worked well ..
      Thank you so much.
Re^3: Round up 3 values to the next 100
by siva kumar (Pilgrim) on Feb 14, 2007 at 07:13 UTC
    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; }

      If you tested that you might be surprised at what it actually does. I'll give you a hint: what does if ( !$var eq '00' ) really mean? (See perlop and look at the table of precedence...)

      You might also want to specify how negative numbers should be rounded. If -12345 should round to -12300, my solution will work but the approach you outlined will produce different results.

Re^3: Round up 3 values to the next 100
by gowthamtr (Sexton) on Feb 14, 2007 at 07:26 UTC
    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