Gossamer Forum
Home : General : Perl Programming :

Number has too many decimal places...

Quote Reply
Number has too many decimal places...
Working on a script that computes a number but the number that is created has too many decimal places. How do I limit it to 2?

Example:
Number I get: 3.05010893246187
Number I want: 3.05

Here is the code I am using:

$ctr = 0;
if ($uclicks > 0){
$ctr = ($uclicks/$uimpress)*100;
}

Appreciate any help!
Thanks,
Ted Monk
Quote Reply
Re: Number has too many decimal places... In reply to
Thanks Mark! It works!

Although it works, just wondering if the code below is correct or should it be written a better way?

$ctr = 0;
if ($uclicks > 0){
$ctr = ($uclicks/$uimpress)*100;
$ctr = sprintf ('%.2f',$ctr);
}

I have also since decided that I don't want any decimal places (50 instead of 50.23). I get my desired result if I put either:

$ctr = sprintf ('%.f',$ctr); or $ctr = sprintf ('%.0f',$ctr); instead of $ctr = sprintf ('%.2f',$ctr);

Both work but is one better than the other?

Thanks again!
Ted

[This message has been edited by Ted Monk (edited November 26, 1999).]
Quote Reply
Re: Number has too many decimal places... In reply to
http://www.gossamer-threads.com/scripts/forum/resources/Forum8/HTML/000884.html

--mark

------------------
Due to repeated requests for support via ICQ, I am once again offering support online.

The ONLY number I will accept requests on is #53788453. Messages sent to my other numbers will not pass through to me, as I have security settings set to ignore messages from users not on that list.

I don't know how often I will be active on this number, as it is for only when I am free to offer assistance.

Could this signature be any longer? :)
Quote Reply
Re: Number has too many decimal places... In reply to
You have:

if ($uclicks > 0){
$ctr = ($uclicks/$uimpress)*100;
}

don't you want:

if ($uimpress > 0){
$ctr = ($uclicks/$uimpress)*100;
}

(I assume you are checking to avoid divide by 0 error)?

Cheers,

Alex
Quote Reply
Re: Number has too many decimal places... In reply to
Mmmm... no, it's the clicks I am calculating. The clicks equal zero unless greater than zero. If they are then I want them divided by the impressions times 100 to give me the CTR.

btw - it all works, I just wanted to limit the decimal points, which I have been able to with Marks suggestion. Now just asking if the new code I added to do this is in the correct place or if there is a better way to write it. For example, I have 2 lines that begin with "ctr=" and thought maybe this could all be on 1 line?

It all works fine, just trying for tighter code if possible. Smile

Thanks,
Ted
Quote Reply
Re: Number has too many decimal places... In reply to
You could condense the entire conditional down to one line if you wish.

Code:
$ctr = sprintf('%.f', (($uclicks/$uimpress) * 100)) if ($uclicks > 0);

The glory of Perl.
-- Gordon --


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

Quote Reply
Re: Number has too many decimal places... In reply to
Great! Thanks to all!