Gossamer Forum
Home : General : Perl Programming :

How to ....

Quote Reply
How to ....
I've did a calculation with some of my links. It looks fine for me, but the problem is that it displays 39.73, etc...
I want that it displays everything below .50 to the lower digit and all above .50 to the higher digit :
39.73 should be 40
39.43 should be 39

Thanks...
Tim
Quote Reply
Re: How to .... In reply to
I'm only learning, but I tried this for an array and it worked so it should be ok. All it's doing is splitting the $value into $before and $after the decimal point. Then if it finds that $after is ABOVE fifty (you might want to make it 49 or 51), it adds 1 to $before, changes $after to 00, puts 'em together and returns the rounded off $finalvalue.

Code:
($before,$after) = split(/\./,$value);
if ($after > 50) { $before++; }
$after = "00";
$finalvalue = "$before\.$after";

Hope it works.

adam
Quote Reply
Re: How to .... In reply to
I would think that int($num) is what you want. If $num is 37.5, 37.6, 37.7, etc., $num = int($num) woould return 38 in $num. If $num is 37.4, 37.3, etc., $num = int($num) would return 37 in $num. Isn't that how the int function works?
Quote Reply
Re: How to .... In reply to
Okay, that could work but !!!
I can't put thousands of lines into the script to just make a 37.9 38 and 37.4 34.

i use the following :
$price_vat = $price * 1.21; (21%)

know i need to know a simple routine that checks the $price_vat and changes it.


,Tim

[This message has been edited by odit (edited March 30, 1999).]
Quote Reply
Re: How to .... In reply to
 
Quote:
If $num is 37.4, 37.3, etc., $num = int($num) would return 37 in $num. Isn't that how the int function works?

Nope, int() doesn't do any rounding. int 37.999 will be 37. It just removes the decimal point.

If you want to do rounding, we refer to the trusty perl faq:

http://language.perl.com/...have_a_round_functio

or alternatively (simpler IMO) add 0.5 to the number before calling int:

$rounded = int ($number + 0.5);

Quote:
know i need to know a simple routine that checks the $price_vat and changes it.

How about:

$price_vat = int ($price_vat + 0.5);

to get a rounded integer.

Cheers,

Alex