Gossamer Forum
Home : General : Perl Programming :

substr(@cent[1], 0, 2)

Quote Reply
substr(@cent[1], 0, 2)
This should round when cents are higher than 50.
Unfortunately it doesn't work if @cent[1] is a value with zero like = @cent[0].60 or .50. ecc.
(It works only for a "full" value like .61, .53 ecc. ) I guess perl is removing zero.

code:
-----
@cent = split/\./,$lit;
@cent[1] = substr(@cent[1], 0, 2);
$lit++ if (@cent[1] > 50);
$lit=~s/.@cent[1]//;

----
Any suggestions to fix this?
Thanks.
Quote Reply
Re: substr(@cent[1], 0, 2) In reply to
Try this:
Code:
$lit = sprintf ('%.2f',$lit);
This will round 2 places past the decimal.

Chris Ellenburg
chris@wfmy.com

------------------
WFMY-TV Webmaster
Quote Reply
Re: substr(@cent[1], 0, 2) In reply to
Try this revised code from before:
Code:
if ($litval == 1) {
$lit = $iltot * 1936.27;
($lit,$cent) = split(/\./, $lit);
$cent = substr($cent, 0, 2);
$cent .= "0" if (length($cent) == 1);
$lit++ if ($cent > 50);
}
-- Gordon --


------------------
$blah='82:84:70:77';
print chr($_) foreach (split/:/,$blah);

Quote Reply
Re: substr(@cent[1], 0, 2) In reply to
Chris:
I get a format like $cent[0].6
I removed 0

Gordon:
I get a server error.

Thanks very much for your help.

Quote Reply
Re: substr(@cent[1], 0, 2) In reply to
Robert:

Forget the whole deal about
Code:
@cent = split/\./,$lit;
@cent[1] = substr(@cent[1], 0, 2);
$lit++ if (@cent[1] > 50);
$lit=~s/.@cent[1]//;

Take the original number, ($lit), which I'm guessing is probably a number like 12.54664. Just take $lit and do what I showed you above.

------------------
WFMY-TV Webmaster