By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
 459,317 Members | 1,265 Online
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,317 IT Pros & Developers. It's quick & easy.

# Need help with rounding on a calculation

 P: n/a Hi, I am a newbie with Javascript. I am trying to build a simple form that calculates the cost of gasoline for a trip. It simply divides the trip distance by the mpg and multiplies that by the current per gallon price. It works, fine except I want to round the number to two decimals ( 64.16, not 64.166666666) Any help is greatly appreciated. Here is what I have so far. I tried to use the toFixed(2), but couldn't figure out where to put it. Price per gallon of gas:
Trip distance in Miles:
MPG for your vehicle:

Gas cost for your trip:  \$ Thank You, Larry Jun 27 '08 #1
Share this Question
9 Replies

 P: n/a On Apr 22, 8:06 pm, "Verizon News Server" wrote: Hi, I am a newbie with Javascript. I am trying to build a simple form that calculates the cost of gasoline for a trip. It simply divides the trip distance by the mpg and multiplies that by the current per gallon price. It works, fine except I want to round the number to two decimals ( 64.16, not 64.166666666) Any help is greatly appreciated. Here is what I have so far. I tried to use the toFixed(2), but couldn't figure out where to put it. Price per gallon of gas:
Trip distance in Miles:
MPG for your vehicle:

Gas cost for your trip:  \$ Thank You, Larry Math.round(x * 100)/100 the following alerts 64.17 alert(Math.round(64.166666666 * 100)/100); Jun 27 '08 #2

 P: n/a On Apr 23, 11:06 am, "Verizon News Server" wrote: Hi, I am a newbie with Javascript. I am trying to build a simple form that calculates the cost of gasoline for a trip. It simply divides the trip distance by the mpg and multiplies that by the current per gallon price. It works, fine except I want to round the number to two decimals ( 64.16, not 64.166666666) Any help is greatly appreciated. The FAQ is good for that: 4.6 How do I convert a Number into a String with exactly 2 decimal places? Here is what I have so far. I tried to use the toFixed(2), but couldn't figure out where to put it. Don't, it's broken in some implementations (see link above). >

 P: n/a Thanks, Doug. Admittedly, I am inept with javascript and in over my head on this simple little project. I don't know how to integrate the math.round into my script. Could you or someone help me place it where it needs to be? Any helps is very appreciated. Thanks in advance. Larry "Doug Gunnoe" wrote: >Hi, I am a newbie with Javascript. I am trying to build a simple formthatcalculates the cost of gasoline for a trip.It simply divides the trip distance by the mpg and multiplies that by thecurrent per gallon price.It works, fine except I want to round the number to two decimals ( 64.16,not 64.166666666)Any help is greatly appreciated. Here is what I have so far. I tried touse the toFixed(2), but couldn't figure out where to put it. Price per gallon of gas:
Trip distance in Miles:
MPG for your vehicle:

Gas cost for your trip:  \$ Thank You, Larry Math.round(x * 100)/100 the following alerts 64.17 alert(Math.round(64.166666666 * 100)/100); Jun 27 '08 #4

 P: n/a On Apr 23, 9:33 am, "Larrythedesigner"

 P: n/a Thank You Doug! I appreciate your help. That did the trick. I am still learning and this helped me understand some of what I learned. Thanks, Larry "Doug Gunnoe" Thanks, Doug.Admittedly, I am inept with javascript and in over my head on this simplelittle project. I don't know how to integrate the math.round into myscript. Could you or someone help me place it where it needs to be?Any helps is very appreciated. Thanks in advance.Larry Sure. And you might want to check out what Rob was saying about checking the values, etc. Like he says, you don't want to divide by zero. Also, JavaScript can be tricky when doing stuff like this because of converting values between strings and numbers. But I see you were using the parseFloat function so you seem to be headed in the right direction in that regard. Anyway, to your question, you would use it something like this var tripcost = tripDistance/mpg*perGallon; d.value = Math.round(tripcost * 100)/100; Jun 27 '08 #6

 P: n/a In comp.lang.javascript message <898cd037-3c3d-4346-9d66-5725a1f22a31@i7 6g2000hsf.googlegroups.com>, Tue, 22 Apr 2008 20:18:20, Doug Gunnoe Math.round(x * 100)/100the following alerts 64.17alert(Math.round(64.166666666 * 100)/100); Those who fail to read the FAQ before posting commonly demonstrate ineptness. Evidently you have not considered the difference between rounding to a multiple 0f 0.01 and rounding to two decimals. Currency outputs should be rounded to the nearest penny or pound (or foreign equivalents), not to the nearest florin (unless large, of course). -- (c) John Stockton, nr London, UK. ?@merlyn.demon.co.uk Turnpike v6.05 MIME. Web " or "" (SonOfRFC1036) Jun 27 '08 #7

 P: n/a In comp.lang.javascript message <8d195c89-548d-4486-8ef5-c04992bb57c4@34 g2000hsh.googlegroups.com>, Wed, 23 Apr 2008 12:30:31, Doug Gunnoe " or "" (SonOfRFC1036) Jun 27 '08 #8

 P: n/a In comp.lang.javascript message <653f34c9-01a2-4535-b296-7e41d21d52d1@e3 9g2000hsf.googlegroups.com>, Wed, 23 Apr 2008 19:11:02, Doug Gunnoe By the way, I will never, ever, ever, ever, never read the shitty FAQ.Never. Never, ever, never. Then, if you continue to post here what you naively think are answers, you will continue to demonstrate your childish obstinacy, and it will continue to be remarked upon. Your mere ignorance and lack of understanding are not necessarily to be ashamed of, provided that obvious steps to remedy them are promptly taken. -- (c) John Stockton, nr London UK. ??*@merlyn.demon.co.uk Turnpike v6.05 MIME. Web

 P: n/a On Apr 27, 7:52 am, Dr J R Stockton

### This discussion thread is closed

Replies have been disabled for this discussion.