Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 11-09-2012, 04:12 PM   PM User | #1
hans_spain
New to the CF scene

 
Join Date: Nov 2012
Posts: 3
Thanks: 3
Thanked 0 Times in 0 Posts
hans_spain is an unknown quantity at this point
Show only two decimals in calculator outcome

Hello Every1,

Having a problem here and seems I´m not able to solve it by myself.

I have a calculator that once an amount is entered it adds 5% to the figure entered each year.It then shows the results for 5, 10 and 15 years.

Have tried ToFixed(2) and Math.round but can´t get it to work. Here is the script for it:

Code:
<script type="text/javascript" src="http://code.jquery.com/jquery-1.8.2.js"></script>
<script type='text/javascript'>//<![CDATA[

$(window).load(function(){
$("#calculate").on('click', function() {
var fee = $("#fee").val();
for (var i=0;i<5;i++) {
fee = fee * 1.05;
}
$("#five").html(fee);
for (var i=0;i<5;i++) {
fee = fee * 1.05;
}
$("#ten").html(fee);
for (var i=0;i<5;i++) {
fee = fee * 1.05;
}
$("#fifteen").html(fee);
});
});

//]]>

</script>

<label>fee</label>
<input type="text" id="fee"><br/>
<button id="calculate">Calculate</button><br/>
<label>five years</label>
<div id="five"></div><br/>
<label>ten years</label>
<div id="ten"></div><br/>
<label>fifteen years</label>
<div id="fifteen"></div>
Have tried it with this:

Code:
    var fee = $("#fee").val();
    //for (var i=0;i<5;i++) {
    // fee = fee * 1.05;
    //}

    var fee5 = Math.round( (fee * Math.pow( fee, 5 ))*100)/100;
    var fee10 = Math.round( (fee * Math.pow( fee, 10 ))*100)/100;
    var fee15 = Math.round( (fee * Math.pow( fee, 15 ))*100)/100;
And still no luck.

Could anyone here point me into the right direction?

Thanks in advance for your time and knowledge,

Greetings from Spain,
Hans
hans_spain is offline   Reply With Quote
Old 11-09-2012, 06:31 PM   PM User | #2
jmrker
Senior Coder

 
jmrker's Avatar
 
Join Date: Aug 2006
Location: FL
Posts: 2,799
Thanks: 30
Thanked 463 Times in 457 Posts
jmrker will become famous soon enough
Question

Quote:
Originally Posted by hans_spain View Post
Hello Every1,

Having a problem here and seems I´m not able to solve it by myself.

I have a calculator that once an amount is entered it adds 5% to the figure entered each year.It then shows the results for 5, 10 and 15 years.

Have tried ToFixed(2) and Math.round but can´t get it to work. Here is the script for it:

...
Have you tried .toFixed(2). JS is case sensitive.
jmrker is offline   Reply With Quote
Old 11-19-2012, 04:02 PM   PM User | #3
hans_spain
New to the CF scene

 
Join Date: Nov 2012
Posts: 3
Thanks: 3
Thanked 0 Times in 0 Posts
hans_spain is an unknown quantity at this point
Hello jmrker,

No result, did use it like this : toFixed(2), even tried it like this : ToFixed(2)

Also tried it on various loations but still no luck

Thanks for your help anyway
hans_spain is offline   Reply With Quote
Old 11-19-2012, 04:44 PM   PM User | #4
niralsoni
Regular Coder

 
Join Date: Mar 2008
Location: London
Posts: 143
Thanks: 3
Thanked 38 Times in 38 Posts
niralsoni is an unknown quantity at this point
Did you tried this? -
Code:
  fee5 = fee5.toFixed(2);
niralsoni is offline   Reply With Quote
Old 11-19-2012, 05:15 PM   PM User | #5
Philip M
Supreme Master coder!

 
Philip M's Avatar
 
Join Date: Jun 2002
Location: London, England
Posts: 17,103
Thanks: 197
Thanked 2,421 Times in 2,399 Posts
Philip M has a spectacular aura aboutPhilip M has a spectacular aura aboutPhilip M has a spectacular aura about
Code:
<script type = "text/javascript">
var fee5 = 100;
for (var i=0; i<5; i++) {
var fee5 = fee5 * 1.05
}
alert (fee5.toFixed(2));

var fee10 = 100;
for (var i=0; i<10; i++) {
var fee10 = fee10 * 1.05
}
alert (fee10.toFixed(2));

var fee15 = 100;
for (var i=0; i<15; i++) {
var fee15 = fee15 * 1.05
}
alert (fee15.toFixed(2));

</script>
Not using a loop:-

Code:
var fee = 100;
var ratepc = 5;
var years = 5;
var fee5 = Math.round(fee*(Math.pow((1+ratepc/100),years))*100)/100;
alert (fee5);
years = 10;
var fee10 = Math.round(fee*(Math.pow((1+ratepc/100),years))*100)/100;
alert (fee10);
years = 15;
var fee15 = Math.round(fee*(Math.pow((1+ratepc/100),years))*100)/100;
alert (fee15);
Another way:-

Code:
var fee = 100;
var ratepc = 5;
var fee5 = calc(5);
alert (fee5);
var fee10 = calc(10);
alert (fee10);
var fee15 = calc(15);
alert (fee15);

function calc(years){
var result = Math.round(fee*(Math.pow((1+ratepc/100),years))*100)/100;
return result
}
__________________

All the code given in this post has been tested and is intended to address the question asked.
Unless stated otherwise it is not just a demonstration.

Last edited by Philip M; 11-19-2012 at 05:39 PM..
Philip M is offline   Reply With Quote
Users who have thanked Philip M for this post:
hans_spain (12-04-2012)
Old 11-19-2012, 08:52 PM   PM User | #6
jmrker
Senior Coder

 
jmrker's Avatar
 
Join Date: Aug 2006
Location: FL
Posts: 2,799
Thanks: 30
Thanked 463 Times in 457 Posts
jmrker will become famous soon enough
Exclamation

Quote:
Originally Posted by hans_spain View Post
Hello jmrker,

No result, did use it like this : toFixed(2), even tried it like this : ToFixed(2)

Also tried it on various loations but still no luck

Thanks for your help anyway
Quote:
Originally Posted by niralsoni View Post
Did you tried this? -
Code:
  fee5 = fee5.toFixed(2);
That should have worked.

If not, show specifically the code you are using.
jmrker is offline   Reply With Quote
Users who have thanked jmrker for this post:
hans_spain (12-04-2012)
Old 11-19-2012, 11:26 PM   PM User | #7
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,565
Thanks: 62
Thanked 4,057 Times in 4,026 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
Probably using it in wrong place.

Should be used *ONLY* when setting the "html" value.

That is, ONLY these places:
Code:
    $("#five").html(fee.toFixed(2));
    for (var i=0;i<5;i++) { fee = fee * 1.05; }
    $("#ten").html(fee.toFixed(2));
    for (var i=0;i<5;i++) {fee = fee * 1.05;}
    $("#fifteen").html(fee.toFixed(2));
__________________
An optimist sees the glass as half full.
A pessimist sees the glass as half empty.
A realist drinks it no matter how much there is.
Old Pedant is offline   Reply With Quote
Old 11-19-2012, 11:34 PM   PM User | #8
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,565
Thanks: 62
Thanked 4,057 Times in 4,026 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
You do have to wonder why people use jQuery for something this simple. Drag in the entire jQuery library to avoid using document.getElementById!

Code:
<html:
<body>
<label>fee</label><input type="text" id="fee"><br/>

<button id="calculate">Calculate</button><br/>

<label>five years</label><div id="y5"></div><br/>
<label>ten years</label><div id="y10"></div><br/>
<label>fifteen years</label><div id="y15"></div>

<script type="text/javascript">
(
  function( )
  {
      document.getElementById("calculate").onclick =
          function( )
          {
              var fee = Number(document.getElementById("fee").value);
              for ( var yr = 1; yr <= 15; ++yr )
              {
                   fee *= 1.05;
                   if ( yr % 5 == 0 ) 
                   {
                       document.getElementById("y" + yr).innerHTML = fee.toFixed(2);
                   } 
              }
         }
  }
)();
</script>
</body>
</html>
__________________
An optimist sees the glass as half full.
A pessimist sees the glass as half empty.
A realist drinks it no matter how much there is.
Old Pedant is offline   Reply With Quote
Users who have thanked Old Pedant for this post:
hans_spain (12-04-2012)
Old 12-04-2012, 05:22 PM   PM User | #9
hans_spain
New to the CF scene

 
Join Date: Nov 2012
Posts: 3
Thanks: 3
Thanked 0 Times in 0 Posts
hans_spain is an unknown quantity at this point
Old Pedant, Thanks

That did it, and learned a bit more thanks to you guys.

Now have another question on the same calulator, is it also possible to have the total result after fifteen years show? The three seperate figures ánd a sort of grand total?

Thank you all for your attention,
Greets from Spain
hans_spain is offline   Reply With Quote
Old 12-04-2012, 06:02 PM   PM User | #10
jmrker
Senior Coder

 
jmrker's Avatar
 
Join Date: Aug 2006
Location: FL
Posts: 2,799
Thanks: 30
Thanked 463 Times in 457 Posts
jmrker will become famous soon enough
Lightbulb

Adding to 'Old Pedant's code...
Code:
<html:
<body>
<label>fee</label><input type="text" id="fee">  
<button id="calculate">Calculate</button><p/>

<label>five years</label> <span id="y5"></span><p/>
<label>ten years</label> <span id="y10"></span><p/>
<label>fifteen years</label> <span id="y15"></span><p/>
Total Fees: <span id="totalFees"></span><p/>

<script type="text/javascript">
( function( ) {
    document.getElementById("calculate").onclick = function( ) {
      var total = 0;
      var fee = Number(document.getElementById("fee").value);
      for ( var yr = 1; yr <= 15; ++yr ) {
        fee *= 1.05;
        if ( yr % 5 == 0 ) {
          document.getElementById("y" + yr).innerHTML = fee.toFixed(2);
          total += fee.toFixed(2) * 1;
        } 
      }
      document.getElementById('totalFees').innerHTML = total.toFixed(2);
    }
  }
)();
</script>
</body>
</html>
jmrker is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 09:24 PM.


Advertisement
Log in to turn off these ads.