Flash Website Builder- Trendy Site Builder is a Flash Site Building tool that helps users build stunning websites. Check Out Custom Custom Logo Design by LogoBee. Website Design and Free Logo Templates available.
 CodingForums.com Show only two decimals in calculator outcome

Before you post, read our: Rules & Posting Guidelines

Enjoy an ad free experience by logging in. Not a member yet? Register.
 11-09-2012, 05: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 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: ```

``` 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
11-09-2012, 07:31 PM   PM User | #2
jmrker
Senior Coder

Join Date: Aug 2006
Location: FL
Posts: 2,935
Thanks: 36
Thanked 477 Times in 471 Posts

Quote:
 Originally Posted by hans_spain 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.

 11-19-2012, 05: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 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
 11-19-2012, 05:44 PM PM User | #4 niralsoni Regular Coder   Join Date: Mar 2008 Location: London Posts: 152 Thanks: 4 Thanked 42 Times in 42 Posts Did you tried this? - Code: ` fee5 = fee5.toFixed(2);`
 11-19-2012, 06:15 PM PM User | #5 Philip M Supreme Master coder!     Join Date: Jun 2002 Location: London, England Posts: 17,475 Thanks: 200 Thanked 2,470 Times in 2,448 Posts Code: `````` 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 06:39 PM..
 Users who have thanked Philip M for this post: hans_spain (12-04-2012)
11-19-2012, 09:52 PM   PM User | #6
jmrker
Senior Coder

Join Date: Aug 2006
Location: FL
Posts: 2,935
Thanks: 36
Thanked 477 Times in 471 Posts

Quote:
 Originally Posted by hans_spain 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 Did you tried this? - Code: ` fee5 = fee5.toFixed(2);`
That should have worked.

If not, show specifically the code you are using.

 Users who have thanked jmrker for this post: hans_spain (12-04-2012)
 11-20-2012, 12:26 AM PM User | #7 Old Pedant Supreme Master coder!     Join Date: Feb 2009 Posts: 24,949 Thanks: 75 Thanked 4,307 Times in 4,274 Posts 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.
 11-20-2012, 12:34 AM PM User | #8 Old Pedant Supreme Master coder!     Join Date: Feb 2009 Posts: 24,949 Thanks: 75 Thanked 4,307 Times in 4,274 Posts 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: ```

``` __________________ 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.
 Users who have thanked Old Pedant for this post: hans_spain (12-04-2012)
 12-04-2012, 06: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 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
 12-04-2012, 07:02 PM PM User | #10 jmrker Senior Coder     Join Date: Aug 2006 Location: FL Posts: 2,935 Thanks: 36 Thanked 477 Times in 471 Posts Adding to 'Old Pedant's code... Code: ```

Total Fees:

```

 Bookmarks

 Thread Tools Rate This Thread Rate This Thread: 5 : Excellent 4 : Good 3 : Average 2 : Bad 1 : Terrible

 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 Rules
 Forum Jump User Control Panel Private Messages Subscriptions Who's Online Search Forums Forums Home :: Client side development     JavaScript programming         DOM and JSON scripting         Ajax and Design         JavaScript frameworks         Post a JavaScript     HTML & CSS     XML     Flash & ActionScript         Adobe Flex     Graphics and Multimedia discussions     General web building         Site reviews         Building for mobile devices :: Server side development     Apache configuration     Perl/ CGI     PHP         Post a PHP snippet     MySQL         Other Databases     Ruby & Ruby On Rails     ASP     ASP.NET     Java and JSP     Other server side languages/ issues         ColdFusion         Python :: Computing & Sciences     Computer Programming     Computer/PC discussions     Geek News and Humour Web Projects and Services Marketplace     Web Projects         Small projects (quick fixes and changes)         Medium projects (new script, new features, etc)         Large Projects (new web application, complex features etc)         Unknown sized projects (request quote)         Vacant job positions         Looking for work/ for hire         Project collaboration/ partnership         Paid work offers and requests (Now CLOSED)     Career, job, and business ideas or advice     Domains, Sites, and Designs for sale         Domains for sale         Websites for sale         Design templates and graphics for sale :: Other forums     Member Offers     Forum feedback and announcements

All times are GMT +1. The time now is 05:35 AM.