Hey all,
It's been a couple of years since my JavaScript class and I have a question. I have JS for figuring out a dollar amount and I can't find a way to have the answer drop to two decimal places without running a second function (I'm looking through my old code but I don't have anything there to help me). Here's what I have:
function doMath() {
var GasPrice = eval(document.Stuff.GasPrice.value)
var Mpg = eval(document.Stuff.Mpg.value)
var TripMiles = eval(document.Stuff.TripMiles.value)
var DaysWeek = eval(document.Stuff.DaysWeek.value)
var GasCost = TripMiles / Mpg * GasPrice * DaysWeek
alert("You're paying $" + GasCost + " to drive back and forth to work each week")
}
I think it'd be easier for people using this "calculator" if the answer didn't go out for 15 decimal places. Is there a way to limit the GasCost value to two decimal places without building a second function? If not, is toFixed the only way to go?
Thanks,
Joe Brouillette
It's been a couple of years since my JavaScript class and I have a question. I have JS for figuring out a dollar amount and I can't find a way to have the answer drop to two decimal places without running a second function (I'm looking through my old code but I don't have anything there to help me). Here's what I have:
function doMath() {
var GasPrice = eval(document.Stuff.GasPrice.value)
var Mpg = eval(document.Stuff.Mpg.value)
var TripMiles = eval(document.Stuff.TripMiles.value)
var DaysWeek = eval(document.Stuff.DaysWeek.value)
var GasCost = TripMiles / Mpg * GasPrice * DaysWeek
alert("You're paying $" + GasCost + " to drive back and forth to work each week")
}
I think it'd be easier for people using this "calculator" if the answer didn't go out for 15 decimal places. Is there a way to limit the GasCost value to two decimal places without building a second function? If not, is toFixed the only way to go?
Thanks,
Joe Brouillette