question from perl noob

B

boblehue

Hi

I'm tryng to learn a little perl and have a problems with decimal's.

This asignment is to rewrite this code to not print more the two decimals
with the use of INT operator, multiplication, and division.

print "Monthly deposit amount? ";
$pmt=<STDIN>;
chomp $pmt;

print "Annual Interest rate? (ex. 7% is .07) ";
$interest=<STDIN>;
chomp $interest;

print "Number of months to deposit? ";
$mons=<STDIN>;
chomp $mons;

# Formula requires a monthly interest
$interest/=12;

$total=$pmt * ( ( (1 + $interest) ** $mons ) -1 )/ $interest;

print "After $mons months, at $interest monthly you\n";
print "Will have $total,\n";


Regards
Boblehue
 
B

boblehue

Hi and tnx Garry!

I tryed your code but it did not give me the result i wanted.

With your code : 29498.282425,

With origninal code : 29498.2824251624

Result i'm looking for : 29498.28

I get closer with your code but not quite!

And i'm suposed to use the INT operator, multiplication, and division.

Regards

Boblehue
 
C

Chris

boblehue said:
Hi

I'm tryng to learn a little perl and have a problems with decimal's.

This asignment is to rewrite this code to not print more the two decimals
with the use of INT operator, multiplication, and division.
<snip>

The trick is to multiply everything up by 100 so that you're working in
pence, cents, or whatever currency you're dealing with. Then do your
monthly interest multiplication forced to INT and then divide you're
final answer by 100 to get back you're 2 dec. points.

Chris.
 
C

Chris

Garry Short wrote:
$total= sprintf "%2f", ($pmt * ( ( (1 + $interest) ** $mons ) -1 )/
$interest);
<snip>

That should be:

$total= sprintf "%.2f", ($pmt * ( ( (1 + $interest) ** $mons ) -1
)/$interest); ^

Your code tries to round the float to two sig. figs not two dec. places.
The dot will remedy that.
 
J

joeri

boblehue said:
Hi and tnx Garry!

I tryed your code but it did not give me the result i wanted.

With your code : 29498.282425,

With origninal code : 29498.2824251624

Result i'm looking for : 29498.28

I get closer with your code but not quite!

And i'm suposed to use the INT operator, multiplication, and division.

Regards

Boblehue

You're right, it should be" %0.2f"

Greets,

J
 
B

boblehue

Or u can use the grey ones and do some math:

$total=$pmt * ( ( ( 1 + $interest) ** $mons ) -1 )/ $interest;
$total=$total * 100;
$total=int($total);
$total=$total/100;

print "After $mons months, at $interest monthly you\n";
print "Will have $total.\n";

I found the solution on my one :)
One step closer to understand perl...

Regards
Kenneth
 
C

Chris

Chris said:
<snip>

The trick is to multiply everything up by 100 so that you're working in
pence, cents, or whatever currency you're dealing with. Then do your
monthly interest multiplication forced to INT and then divide you're
final answer by 100 to get back you're 2 dec. points.

Chris.
Please excuse the piss-poor grammar. The last two "you're"s should be
"your".
 
C

Chris Mattern

boblehue said:
Hi and tnx Garry!

I tryed your code but it did not give me the result i wanted.

With your code : 29498.282425,

With origninal code : 29498.2824251624

Result i'm looking for : 29498.28

I get closer with your code but not quite!

And i'm suposed to use the INT operator, multiplication, and division.
And just why are you limited to those functions?

I smell homework.

Chris Mattern
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,121
Messages
2,570,712
Members
47,283
Latest member
hopkins1988

Latest Threads

Top