|
From: Verizon News Server on 22 Apr 2008 21:06 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. <head> <title></title> <script type="text/javascript"> function calcTripCost(){ var a = document.getElementById('perGallon'); var b = document.getElementById('tripDistance'); var c = document.getElementById('mpg'); var d = document.getElementById('tripCost'); var perGallon = parseFloat (a.value); var tripDistance = parseFloat (b.value); var mpg = parseFloat (c.value); var tripCost = parseFloat (d.value); d.value = tripDistance/mpg*perGallon; } </script> </head> <body> Price per gallon of gas: <input type="text" id="perGallon" name="perGallon" value="3.85" /> <br /> Trip distance in Miles: <input type="text" id="tripDistance" name="tripDistance" value="300" /><br /> MPG for your vehicle: <input type="text" id="mpg" name="mpg" value="18" /><br/><br/> <input type="button" value="Calculate" onclick="calcTripCost();" /> <br /> Gas cost for your trip: $ <input type="text" id="tripCost" /> </body> Thank You, Larry
From: Doug Gunnoe on 22 Apr 2008 23:18 On Apr 22, 8:06 pm, "Verizon News Server" <anonymous(a)no_spam.info> 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. > > <head> > <title></title> > <script type="text/javascript"> > function calcTripCost(){ > var a = document.getElementById('perGallon'); > var b = document.getElementById('tripDistance'); > var c = document.getElementById('mpg'); > var d = document.getElementById('tripCost'); > var perGallon = parseFloat (a.value); > var tripDistance = parseFloat (b.value); > var mpg = parseFloat (c.value); > var tripCost = parseFloat (d.value); > d.value = tripDistance/mpg*perGallon; > > } > </script> > </head> > <body> > Price per gallon of gas: <input type="text" id="perGallon" > name="perGallon" > value="3.85" /> > <br /> > Trip distance in Miles: <input type="text" id="tripDistance" > name="tripDistance" > value="300" /><br /> > MPG for your vehicle: <input type="text" id="mpg" name="mpg" value="18" > /><br/><br/> > <input type="button" value="Calculate" onclick="calcTripCost();" /> <br > /> > Gas cost for your trip: $ <input type="text" id="tripCost" /> > > </body> > > Thank You, Larry Math.round(x * 100)/100 the following alerts 64.17 alert(Math.round(64.166666666 * 100)/100);
From: RobG on 22 Apr 2008 23:22 On Apr 23, 11:06 am, "Verizon News Server" <anonymous(a)no_spam.info> 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? <URL: http://www.jibbering.com/faq/#FAQ4_6 > > 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). > > <head> > <title></title> > <script type="text/javascript"> > function calcTripCost(){ > var a = document.getElementById('perGallon'); > var b = document.getElementById('tripDistance'); > var c = document.getElementById('mpg'); > var d = document.getElementById('tripCost'); > var perGallon = parseFloat (a.value); > var tripDistance = parseFloat (b.value); > var mpg = parseFloat (c.value); > var tripCost = parseFloat (d.value); > d.value = tripDistance/mpg*perGallon; You should test values before using them for arithmetic operations to make sure they are numbers within a suitable range. At the very least, check that mpg is not zero. -- Rob
From: Larrythedesigner on 23 Apr 2008 10:33 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" <douggunnoe(a)gmail.com> wrote in message news:898cd037-3c3d-4346-9d66-5725a1f22a31(a)i76g2000hsf.googlegroups.com... > On Apr 22, 8:06 pm, "Verizon News Server" <anonymous(a)no_spam.info> > 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. >> >> <head> >> <title></title> >> <script type="text/javascript"> >> function calcTripCost(){ >> var a = document.getElementById('perGallon'); >> var b = document.getElementById('tripDistance'); >> var c = document.getElementById('mpg'); >> var d = document.getElementById('tripCost'); >> var perGallon = parseFloat (a.value); >> var tripDistance = parseFloat (b.value); >> var mpg = parseFloat (c.value); >> var tripCost = parseFloat (d.value); >> d.value = tripDistance/mpg*perGallon; >> >> } >> </script> >> </head> >> <body> >> Price per gallon of gas: <input type="text" id="perGallon" >> name="perGallon" >> value="3.85" /> >> <br /> >> Trip distance in Miles: <input type="text" id="tripDistance" >> name="tripDistance" >> value="300" /><br /> >> MPG for your vehicle: <input type="text" id="mpg" name="mpg" >> value="18" >> /><br/><br/> >> <input type="button" value="Calculate" onclick="calcTripCost();" /> >> <br >> /> >> Gas cost for your trip: $ <input type="text" id="tripCost" /> >> >> </body> >> >> Thank You, Larry > > Math.round(x * 100)/100 > > the following alerts 64.17 > alert(Math.round(64.166666666 * 100)/100); > >
From: Doug Gunnoe on 23 Apr 2008 15:30 On Apr 23, 9:33 am, "Larrythedesigner" <anonymous(a)no_spam.info> wrote: > 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 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;
|
Next
|
Last
Pages: 1 2 Prev: JS menu/iframe conflict Next: getting computed style for Img width and height |