|
|
|||||||||
|
|||||||||
| |||||||||
|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
|
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
|
|||
|
|||
|
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 |
|
#2
|
||||
|
||||
|
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! |
|
#3
|
|||
|
|||
|
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 |
|
#4
|
||||
|
||||
|
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] |
|
#5
|
|||
|
|||
|
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 |
|
#6
|
|||
|
|||
|
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 |
|
#7
|
|||
|
|||
|
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 |
|
#8
|
|||
|
|||
|
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. |
![]() |
| Viewing: Dev Shed Forums > Programming Languages > Perl Programming > rounding a decimal number to the upper value |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|