Perl Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsProgramming LanguagesPerl Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old April 16th, 2001, 12:57 PM
meeh82 meeh82 is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Sep 2000
Location: Riverside, CA
Posts: 63 meeh82 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 9
I need to round a number, but I cannot find a function to round a number.

Ex: 156.8867
needs to be
156.89



Thanks,
Jonathan Donaghe

Reply With Quote
  #2  
Old April 16th, 2001, 01:40 PM
unobserved unobserved is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Mar 2001
Posts: 11 unobserved User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Send a message via ICQ to unobserved
Code:
my $longNumber = 156.8867;
my $roundedNumber = sprintf "%.2f", $longNumber;
print "The rounded number is : $roundedNumber\n";


Should round the number and print it to screen.
sprintf is the command to use, the '2' in the arguments is the number of decimal places to round the number to. A '4' instead of the '2' would round to 4 decimals places.

Reply With Quote
  #3  
Old April 16th, 2001, 01:52 PM
meeh82 meeh82 is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Sep 2000
Location: Riverside, CA
Posts: 63 meeh82 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 9
I have this in place, but does not round the number

I have the number 976.755 and it rounds it to 976.75


Is there another way to round it?

Thanks,
Jonathan Donaghe

Reply With Quote
  #4  
Old April 16th, 2001, 02:00 PM
unobserved unobserved is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Mar 2001
Posts: 11 unobserved User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Send a message via ICQ to unobserved
Opps, sorry, forgot a line. You should probably make this into a subroutine like this.

Code:
sub roundToTwoPlaces {
 my $inptNumber = shift;
 my $roundedNumber = sprintf "%.2f", $inptNumber;
 $roundedNumber += .01;
 return $roundedNumber;
}

AND/OR

sub roundToFourPlaces {
 my $inptNumber = shift;
 my $roundedNumber = sprintf "%.4f", $inptNumber;
 $roundedNumber += .0001;
 return $roundedNumber;
}


and then just call it with:
my $number = roundToTwoPlaces('976.755');

Hope that helps.

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesPerl Programming > How can I round a number


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 1 hosted by Hostway
Stay green...Green IT