Beefy Boxes and Bandwidth Generously Provided by pair Networks kudra
Perl Sensitive Sunglasses
 
PerlMonks

Re: Round up 3 values to the next 100

by bobf (Prior)
 | Log in | Create a new user | The Monastery Gates | Super Search | 
 | Seekers of Perl Wisdom | Meditations | PerlMonks Discussion | 
 | Obfuscation | Reviews | Cool Uses For Perl | Perl News | Q&A | Tutorials | 
 | Poetry | Recent Threads | Newest Nodes | Donate | What's New | 

on Feb 14, 2007 at 06:45 UTC ( #599865=note: print w/ replies, xml ) Need Help??

in reply to Round up 3 values to the next 100

See perlfaq4: "Does Perl have a round() function? What about ceil() and floor()? Trig functions?" In particular, take a look at the ceil function in POSIX.

Update: Don't forget about Super Search, which found a particularly relevant thread: Rounding up nearest ten, hundred etc. :-)


Comment on Re: Round up 3 values to the next 100
Download Code
Re^2: Round up 3 values to the next 100
by siva kumar (Pilgrim) on Feb 14, 2007 at 06:59 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.

        Thank you 'bobf'. Worked well ..
        Thank you so much.
      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.

      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
Login:
Password
remember me
What's my password?
Create A New User

Node Status?
node history
Node Type: note [id://599865]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others lurking in the Monastery: (24)
ikegami
GrandFather
Limbic~Region
CountZero
marto
jmcnamara
toolic
holli
Gavin
atcroft
kennethk
thezip
Eyck
Marshall
socketdave
superfrink
crashtest
pemungkah
ssandv
Rodster001
CColin
MikeDexter
johnvk
im2
As of 2010-02-09 21:37 GMT
Sections?
The Monastery Gates
Seekers of Perl Wisdom
Meditations
PerlMonks Discussion
Categorized Q&A
Tutorials
Obfuscated Code
Perl Poetry
Cool Uses for Perl
Perl News
Information?
PerlMonks FAQ
Guide to the Monastery
What's New at PerlMonks
Voting/Experience System
Tutorials
Reviews
Library
Perl FAQs
Other Info Sources
Find Nodes?
Nodes You Wrote
Super Search
List Nodes By Users
Newest Nodes
Recently Active Threads
Selected Best Nodes
Best Nodes
Worst Nodes
Saints in our Book
Leftovers?
The St. Larry Wall Shrine
Offering Plate
Awards
Craft
Snippets Section
Code Catacombs
Quests
Editor Requests
Buy PerlMonks Gear
PerlMonks Merchandise
Planet Perl
Perlsphere
Use Perl
Perl.com
Perl 5 Wiki
Perl Jobs
Perl Mongers
Perl Directory
Perl documentation
CPAN
Random Node
Voting Booth?

What level of existential comfort do you require?

Palace
Executive suite at the best hotel
Regular hotel in a decent part of town
Motel
Boarding house
Sleeping Bag on Couch in Basement
Any port in a storm
Camping under the freeway overpass
Jail
Other

Results (281 votes), past polls