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:
SlickEdit: Code in over 40 languages across 7 platforms. SlickEdit’s unmatched power, speed, and flexibility allows even the most accomplished developers to write better code faster. Download a free trial today!
  #1  
Old February 26th, 2001, 10:48 PM
Dups Dups is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jan 2001
Posts: 30 Dups User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 8
Unhappy

Hello there,
Could anyone please suggest a way to round a no: in a way similar to that of ceil in C. That is I want to get 10 as output for any input between 9 and 10.
I tried using int. But it works the reverse way.
Please help.

Could u please suggest a good site for learning Perl.

Thank u for all your help.
Dups

Reply With Quote
  #2  
Old February 26th, 2001, 11:16 PM
mickalo's Avatar
mickalo mickalo is offline
Ole` Timer
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2000
Location: N.W. Iowa
Posts: 469 mickalo User rank is Private First Class (20 - 50 Reputation Level)mickalo User rank is Private First Class (20 - 50 Reputation Level) 
Time spent in forums: 5 h 19 sec
Reputation Power: 8
Send a message via AIM to mickalo Send a message via MSN to mickalo
Thumbs up

Try something like this:
Code:
$val = "9.45";
$rd_value = int(($val) + 1);


Now the $rd_value will equal 10.

Hope this helps,

Mickalo
__________________

Thunder Rain Internet Publishing

Custom Programming & Database development
Providing Personal/Business
Internet Solutions that work!

Reply With Quote
  #3  
Old February 26th, 2001, 11:56 PM
Dups Dups is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jan 2001
Posts: 30 Dups User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 8
Hello there,

Thank u Mickalo for ur quick reply. I had tried your solution, but it creates problem when $val=10
Then the value returned by your solution is 11, but i require 10 then also.
Please help.
Also please suggest some good sites for learning Perl.

Thank u again.
Dups

Reply With Quote
  #4  
Old February 27th, 2001, 09:08 AM
mickalo's Avatar
mickalo mickalo is offline
Ole` Timer
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2000
Location: N.W. Iowa
Posts: 469 mickalo User rank is Private First Class (20 - 50 Reputation Level)mickalo User rank is Private First Class (20 - 50 Reputation Level) 
Time spent in forums: 5 h 19 sec
Reputation Power: 8
Send a message via AIM to mickalo Send a message via MSN to mickalo
Thumbs up

Code:
$val = "9.45";
$rd_value = '';

@{$sub_val} = split('.',$val);
if ($sub_val->[1] > 00) {
$rd_value = int(($val) + 1);
 } else {
$rd_value = $val;
 }


This should do it!

As far as a Perl resources, you can try http://www.cgi-resources.com CGI Resources , http://www.hotscripts.com HotScripts, right here at http://www.devshed.com Devsheld. They all have some excellent tutotials, links to Perl info.


Mickalo

[Edited by mickalo on 02-27-2001 at 08:12 AM]

Reply With Quote
  #5  
Old February 27th, 2001, 08:26 PM
tigris tigris is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2001
Location: Australia
Posts: 0 tigris 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 tigris
this should work also

assuming that $val is definately going to have a float value, ie: it cant be equal to "10" or "10." it must be "10.00", then the following will work

$val = "9.45";
$rd_value = (split(/\./, $val))[-1] > 0 ? int($val) + 1 : int($val);

cheers
Danial

Reply With Quote
  #6  
Old February 28th, 2001, 12:53 AM
JonLed JonLed is offline
Contributing User
Dev Shed Novice (500 - 999 posts)
 
Join Date: Aug 2000
Location: Indiana
Posts: 614 JonLed User rank is Corporal (100 - 500 Reputation Level)JonLed User rank is Corporal (100 - 500 Reputation Level)JonLed User rank is Corporal (100 - 500 Reputation Level)JonLed User rank is Corporal (100 - 500 Reputation Level) 
Time spent in forums: 4 h 49 m 49 sec
Reputation Power: 9
You guys are all analyzing the number as if it where a string... which takes extra processing. Do something like this instead:
Code:
$value = (int($val) != $val) ? int($val) + 1 : $val;

This assumes that $val holds your number and that it is actually a number (you may want to add $val += 0; above it to ensure that it is a number). Now $value holds your correct number

Reply With Quote
  #7  
Old February 28th, 2001, 01:11 AM
Dups Dups is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jan 2001
Posts: 30 Dups User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 8
Thank u very much JohnLed.
Also I thank tigris and mickalo.

I was thinking whether is a single function which does this.
I think there is not.

Thanks again.

Dups

Reply With Quote
  #8  
Old February 28th, 2001, 08:58 AM
JonLed JonLed is offline
Contributing User
Dev Shed Novice (500 - 999 posts)
 
Join Date: Aug 2000
Location: Indiana
Posts: 614 JonLed User rank is Corporal (100 - 500 Reputation Level)JonLed User rank is Corporal (100 - 500 Reputation Level)JonLed User rank is Corporal (100 - 500 Reputation Level)JonLed User rank is Corporal (100 - 500 Reputation Level) 
Time spent in forums: 4 h 49 m 49 sec
Reputation Power: 9
Well you can use sprintf if you'd like, I guess I didn't think of that before:
Code:
$value = sprintf("%2f", $number)

This would give you a padded number to 2 places before the decimal and none after. But once again, you'll have to figure out if it was an int in the first place like we did before.

The method I stated above is the best. It's simple and will work every time without treating the number like a string.

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesPerl Programming > rounding a decimal number to the upper value


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 3 hosted by Hostway