Ask a Question related to PERL Miscellaneous, Design and Development.
-
boblehue #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
-
Noob question about the server
Hi guys and gals, Ok, I am a new to all of this so help me out here if you dont mind. I have a presentation on flash which will envolve video. ... -
3d text. noob question
Hi. Part of the logo for my school is text on its side. When i make this text 3d, director puts it back the right way up and then i cant rotate... -
sorry noob question
i wanna make a 3d game but how do i make a world? should i make 1 sprite and put models in em(if thats possible please tell me how) or 1 sprite for... -
Stupid Noob Question
Im trying to fill a shape but I cant seem to combine it. I drew it using the bezigon and arc tools and I want to fill the shape, its basically like... -
Noob question
this probably sounds stupid, but i was just wondering if FireWorks and Photoshop are basically the same? cause I noe how to do basics on PS7 but im... -
boblehue #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 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_sh0rt@zw4llet.com> wrote in message
news:bgd89m$3lv$1$8302bc10@news.demon.co.uk...> boblehue wrote:
>
> <SNIP>> <SNIP>> > $total=$pmt * ( ( (1 + $interest) ** $mons ) -1 )/ $interest;
> >>> >
> > 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
-
Chris #3
Re: question from perl noob
boblehue wrote:
<snip>> 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.
>
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
-
Chris #4
Re: question from perl noob
Garry Short wrote:
<snip>
<snip>> $total= sprintf "%2f", ($pmt * ( ( (1 + $interest) ** $mons ) -1 )/
> $interest);
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
-
joeri #5
Re: question from perl noob
"boblehue" <boblehue@online.no> wrote in message
news:YZpWa.17199$Hb.287974@news4.e.nsc.no...You're right, it should be" %0.2f"> 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_sh0rt@zw4llet.com> wrote in message
> news:bgd89m$3lv$1$8302bc10@news.demon.co.uk...>> > boblehue wrote:
> >
> > <SNIP>> > <SNIP>> > > $total=$pmt * ( ( (1 + $interest) ** $mons ) -1 )/ $interest;
> > >> >> > >
> > > 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
> >
> >
>
Greets,
J
joeri Guest
-
boblehue #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" <boblehue@online.no> wrote in message
news:YZpWa.17199$Hb.287974@news4.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_sh0rt@zw4llet.com> wrote in message
> news:bgd89m$3lv$1$8302bc10@news.demon.co.uk...>> > boblehue wrote:
> >
> > <SNIP>> > <SNIP>> > > $total=$pmt * ( ( (1 + $interest) ** $mons ) -1 )/ $interest;
> > >> >> > >
> > > 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
-
Chris #7
Re: question from perl noob
Chris wrote:
Please excuse the piss-poor grammar. The last two "you're"s should be> boblehue wrote:
>> <snip>>> 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.
>>
>
> 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.
>
"your".
Chris Guest
-
Tintin #8
Re: question from perl noob
"boblehue" <boblehue@online.no> wrote in message
news:NdpWa.17188$Hb.287897@news4.e.nsc.no...
[snipped repeated question in alt.perl]
Please read [url]http://www.cs.tut.fi/~jkorpela/usenet/xpost.html[/url]
Tintin Guest
-
Chris Mattern #9
Re: question from perl noob
boblehue wrote:
And just why are you limited to those functions?> 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.
>
I smell homework.
Chris Mattern
Chris Mattern Guest
-
David Oswald #10
Re: question from perl noob
"boblehue" <boblehue@online.no> wrote in message
news:NdpWa.17188$Hb.287897@news4.e.nsc.no...This takes me back to week one of high school Computer Science on the old> 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.
>
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:
Here's the wrong answer: 1234.56789
Here's the right answer: 1234.56
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 parse
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



Reply With Quote

