Professional Web Applications Themes

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

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

  5. #5

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

    Chris Guest

  8. #8

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

    Please read [url]http://www.cs.tut.fi/~jkorpela/usenet/xpost.html[/url]


    Tintin Guest

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

  10. #10

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

Similar Threads

  1. Noob question about the server
    By vbMarkO in forum Macromedia Flash Flashcom
    Replies: 0
    Last Post: July 20th, 08:18 PM
  2. 3d text. noob question
    By Whitestarisligerzero in forum Macromedia Director 3D
    Replies: 1
    Last Post: October 8th, 09:30 PM
  3. sorry noob question
    By the rebel in forum Macromedia Director 3D
    Replies: 3
    Last Post: September 26th, 12:17 PM
  4. Stupid Noob Question
    By slowlrner in forum Macromedia Freehand
    Replies: 3
    Last Post: February 27th, 05:33 AM
  5. Noob question
    By drew146 webforumsuser@macromedia.com in forum Macromedia Fireworks
    Replies: 1
    Last Post: October 5th, 11:24 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not 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