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 Rounding decimals

Before you post, read our: Rules & Posting Guidelines

Enjoy an ad free experience by logging in. Not a member yet? Register.
 07-05-2013, 08:52 PM PM User | #1 iflyrocketships New to the CF scene   Join Date: Jul 2013 Posts: 1 Thanks: 0 Thanked 0 Times in 0 Posts Rounding decimals I am trying to get 10% of standardPremium. Say 500.5 is entered for standardPremium and the answer is 50.050000000000004 I want it round to 50.05. How would I do this? Code: var excessPremium_Percent = 0.1; var input = function (standard_premium,incurred_losses) { var standard_premium = document.getElementById("SPnum").value; var incurred_losses = document.getElementById("ILnum").value; document.getElementById("beginning_funds").innerHTML = standard_premium*excessPremium_Percent; return standard_premium; return incurred_losses; };
 07-05-2013, 09:20 PM PM User | #2 Old Pedant Supreme Master coder!     Join Date: Feb 2009 Posts: 24,949 Thanks: 75 Thanked 4,307 Times in 4,274 Posts Code: document.getElementById("beginning_funds").innerHTML = ( standard_premium*excessPremium_Percent ).toFixed(2); But you have a major problem in your function code: You can't return two different values from a single function. When you do Code: return standard_premium; return incurred_losses; only the *FIRST* return statement returns anything. THE FUNCTION ENDS IMMEDIATELY with that return. And you also made an error in the declaration of your function: Code: var input = function (standard_premium,incurred_losses) { var standard_premium = document.getElementById("SPnum").value; var incurred_losses = document.getElementById("ILnum").value; Because you used var to declare NEW VARIABLES standard_premium and incurred_losses the declaration of those as arguments to your function (in red) is IGNORED! Finally, what is the point is getting the value of incurred_losses when you never use it? __________________ 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.
 07-05-2013, 10:28 PM PM User | #3 felgall Master Coder     Join Date: Sep 2005 Location: Sydney, Australia Posts: 6,094 Thanks: 0 Thanked 579 Times in 569 Posts You can return as many values as you like from a function as long as you return them as either an array or as an object. For example: return [standard_premium, incurred_losses]; or return {sp: standard_premium, il: incurred_losses}; __________________ Stephen Learn Modern JavaScript - http://javascriptexample.net/ Helping others to solve their computer problem at http://www.felgall.com/ Beginners need to advise whether they want to learn "Latin" JavaScript for Netscape 3 or "Italian" JavaScript for modern browsers.
 07-05-2013, 10:40 PM PM User | #4 Old Pedant Supreme Master coder!     Join Date: Feb 2009 Posts: 24,949 Thanks: 75 Thanked 4,307 Times in 4,274 Posts Yes, but either of those is still only a single "value" in the sense that the array or object is a single unit. I didn't bother mentioning that because I figured this was a real newbie, given the other errors and the level of the question. __________________ 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.
 07-06-2013, 12:46 AM PM User | #5 007julien Regular Coder   Join Date: May 2012 Location: France Posts: 174 Thanks: 0 Thanked 27 Times in 25 Posts After these off-topic developments and judgements and to answer specifically to the question, there is two methods to round a number with two decimals : Math.round(myValue*100)/100 and myValue.toFixed(2). The first one gives a number as result with no more as two decimals. The second gives a String with two zero or non-zero digits after the points.
 07-06-2013, 01:02 AM PM User | #6 Old Pedant Supreme Master coder!     Join Date: Feb 2009 Posts: 24,949 Thanks: 75 Thanked 4,307 Times in 4,274 Posts Yes, Julien, but from the context of his code: Code: document.getElementById("beginning_funds").innerHTML = standard_premium*excessPremium_Percent; It was pretty clear that he wanted the innerHTML to display as two decimal places. If he used your first version of rounding, then if the answer happened to be 57.00 it would only display as 57, with no decimals. So the mathematical rounding doesn't do any good for display purposes. So I *did* answer him: Code: document.getElementById("beginning_funds").innerHTML = ( standard_premium*excessPremium_Percent ).toFixed(2); And the rest of my answer may have been irrelevant to his question, but I don't think it is "off topic" with regards to the rest of his JavaScript coding. __________________ 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.

 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 09:59 AM.