# question from perl noob - PERL Miscellaneous

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 ...

1. ## question from perl noob

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

boblehue Guest

2. ## Re: question from perl noob

Hi and tnx Garry!

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

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

"Garry Short" <g4rry_sh0rtzw4llet.com> wrote in message
news:bgd89m\$3lv\$1\$8302bc10news.demon.co.uk...
> boblehue wrote:
>
> <SNIP>
> > \$total=\$pmt * ( ( (1 + \$interest) ** \$mons ) -1 )/ \$interest;
> >
> <SNIP>
> >
> > Regards
> > Boblehue
>
> Not 100% sure, but I'm almost certain that's in the FAQ!
>
> However, modifying your code *very* slightly ...
>
> \$total= sprintf "%2f", (\$pmt * ( ( (1 + \$interest) ** \$mons ) -1 )/
> \$interest);
>
> HTH,
>
> Garry
>
>

boblehue Guest

3. ## Re: question from perl noob

boblehue wrote:
> 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.

Chris Guest

4. ## Re: question from perl noob

Garry Short wrote:
<snip>
> \$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.

Chris Guest

5. ## Re: question from perl noob

"boblehue" <boblehueonline.no> wrote in message
news:YZpWa.17199\$Hb.287974news4.e.nsc.no...
> 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
>
>
>
>
>
> "Garry Short" <g4rry_sh0rtzw4llet.com> wrote in message
> news:bgd89m\$3lv\$1\$8302bc10news.demon.co.uk...
> > boblehue wrote:
> >
> > <SNIP>
> > > \$total=\$pmt * ( ( (1 + \$interest) ** \$mons ) -1 )/ \$interest;
> > >
> > <SNIP>
> > >
> > > Regards
> > > Boblehue
> >
> > Not 100% sure, but I'm almost certain that's in the FAQ!
> >
> > However, modifying your code *very* slightly ...
> >
> > \$total= sprintf "%2f", (\$pmt * ( ( (1 + \$interest) ** \$mons ) -1 )/
> > \$interest);
> >
> > HTH,
> >
> > Garry
> >
> >
>
>
You're right, it should be" %0.2f"

Greets,

J

joeri Guest

6. ## Re: question from perl noob

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

"boblehue" <boblehueonline.no> wrote in message
news:YZpWa.17199\$Hb.287974news4.e.nsc.no...
> 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
>
>
>
>
>
> "Garry Short" <g4rry_sh0rtzw4llet.com> wrote in message
> news:bgd89m\$3lv\$1\$8302bc10news.demon.co.uk...
> > boblehue wrote:
> >
> > <SNIP>
> > > \$total=\$pmt * ( ( (1 + \$interest) ** \$mons ) -1 )/ \$interest;
> > >
> > <SNIP>
> > >
> > > Regards
> > > Boblehue
> >
> > Not 100% sure, but I'm almost certain that's in the FAQ!
> >
> > However, modifying your code *very* slightly ...
> >
> > \$total= sprintf "%2f", (\$pmt * ( ( (1 + \$interest) ** \$mons ) -1 )/
> > \$interest);
> >
> > HTH,
> >
> > Garry
> >
> >
>
>

boblehue Guest

7. ## Re: question from perl noob

Chris wrote:
> boblehue wrote:
>
>> 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.
>
Please excuse the -poor grammar. The last two "you're"s should be
"your".

Chris Guest

8. ## Re: question from perl noob

"boblehue" <boblehueonline.no> wrote in message
news:NdpWa.17188\$Hb.287897news4.e.nsc.no...

[snipped repeated question in alt.perl]

Tintin Guest

9. ## Re: question from perl noob

boblehue wrote:
> 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

Chris Mattern Guest

10. ## Re: question from perl noob

"boblehue" <boblehueonline.no> wrote in message
news:NdpWa.17188\$Hb.287897news4.e.nsc.no...
> 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.
>
This takes me back to week one of high school Computer Science on the old
Franklin Ace 1000's (Apple II+ clones).

If we were doing it in BASIC (why not?), here's an example snippet that
should illustrate what your teacher hoped you would figure out on your own.

let X = 1234.56789
print "Here's the wrong answer: ";X
X = int ( X * 100 ) / 100
print "Here's the right answer: ";X
end

The output of the above example will be:

What you did:
Multiply by one hundred, thus shifting the decimal place to the right two
places.
Chop off everything to the right of the decimal place.
Divide by 100, moving the decimal place back to the left two places, where
it originally was.

Now in Perl:

my \$x = 1234.56789;
print "Here is the wrong answer: \$x\n";
\$x = int ( \$x * 100 ) / 100 ;
print "Here is the right answer: \$x\n";

Remembering back to my old BASIC days, the most common use of int() was in
conjunction with rnd(1), in which case you could derive a random number
between zero and whatever you need as follows:

let X = int(rnd(0)*500)+1
rem X is now a pseudorandom number between 1 and 500.

Why am I telling you all this in BASIC? I guess just for fun, and because
your question reminds me of Mr. Secrest's High School Comp. Sci. class in
1985. ...and also because the other day I was using Perl to write a BASIC
intrepreter just to see if I could do it. ...didn't bother finishing, I was
mostly just interested in thinking through the process of using hashes to
hold BASIC lexicon, hashes for namespace, and thinking through how to p
it all. It was a fun exercise, but nothing more because first, someone
already wrote a perfectly good BASIC module for Perl, and second, who would
want to use a perfectly good language (Perl) to mimick a perfectly lousy one
(BASIC)?

Dave

David Oswald Guest

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•