question from perl noob

Ask a Question related to PERL Miscellaneous, Design and Development.

  1. #1

    Default 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. Similar Questions and Discussions

    1. 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. ...
    2. 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...
    3. 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...
    4. 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...
    5. 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...
  3. #2

    Default 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>
    > > $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

  4. #3

    Default 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

  5. #4

    Default 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

  6. #5

    Default Re: question from perl noob


    "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>
    > > > $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

  7. #6

    Default 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>
    > > > $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

  8. #7

    Default 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 piss-poor grammar. The last two "you're"s should be
    "your".

    Chris Guest

  9. #8

    Default 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

  10. #9

    Default 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

  11. #10

    Default Re: question from perl noob


    "boblehue" <boblehue@online.no> wrote in message
    news:NdpWa.17188$Hb.287897@news4.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:
    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

Posting Permissions

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

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139