Need help with rounding on a calculation

  • Thread starter Verizon News Server
  • Start date
V

Verizon News Server

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" />&nbsp;&nbsp;
<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:&nbsp; $ <input type="text" id="tripCost" />

</body>

Thank You, Larry
 
D

Doug Gunnoe

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" />&nbsp;&nbsp;
<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:&nbsp; $ <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);
 
R

RobG

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).
<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.
 
L

Larrythedesigner

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
 
D

Dr J R Stockton

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)/100

the following alerts 64.17
alert(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).
 
D

Doug Gunnoe

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;
 
L

Larrythedesigner

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
 
D

Doug Gunnoe

Those who fail to read the FAQ before posting commonly demonstrate
ineptness.

People with Dr. in front of their name commonly demonstrate their
ability to harp on meaningless trivial bullshit, with no consideration
at all for anything practical. If you had actually bothered to read
the OP he was simply wanting to format the output rounded to two
decimals exactly like I showed him. The purpose of his exercise has
nothing to do with some sideline nonsense about properly rounding
currency, but is rather about the OP practicing a little with
javascript. But whatever, if you actually gave a shit you could have
answered his question instead of criticizing my answer.

By the way, I will never, ever, ever, ever, never read the shitty FAQ.
Never. Never, ever, never.


Never.
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).

..01 is a penny.
..10 is a dime
1.00 is a dollar

But thanks anyway for your useless .02
 
E

Evertjan.

Doug Gunnoe wrote on 24 apr 2008 in comp.lang.javascript:
People with Dr. in front of their name commonly demonstrate their
ability to harp on meaningless trivial bullshit, with no consideration
at all for anything practical. If you had actually bothered to read
the OP he was simply wanting to format the output rounded to two
decimals exactly like I showed him. The purpose of his exercise has
nothing to do with some sideline nonsense about properly rounding
currency, but is rather about the OP practicing a little with
javascript. But whatever, if you actually gave a shit you could have
answered his question instead of criticizing my answer.

By the way, I will never, ever, ever, ever, never read the shitty FAQ.
Never. Never, ever, never.


Never.


.01 is a penny.
.10 is a dime
1.00 is a dollar

But thanks anyway for your useless .02

Showing your ineptness in following good advice is one thing,
showing your ignorance of the errors in
rounding a binary fraction for decimal rounded output is worse.

Please read tha faq and repair your manners or be prepared to be ploncked.
 
D

Dr J R Stockton

In comp.lang.javascript message <8d195c89-548d-4486-8ef5-c04992bb57c4@34
g2000hsh.googlegroups.com>, Wed, 23 Apr 2008 12:30:31, Doug Gunnoe
But I see
you were using the parseFloat function so you seem to be headed in the
right direction in that regard.

Function parseFloat is not needed in that code. FAQ 4.21.
 
D

Dr J R Stockton

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.
 
D

Doug Gunnoe

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.

lol.

Remark away.
Your mere ignorance and lack of understanding are not necessarily to be
ashamed of, provided that obvious steps to remedy them are promptly
taken.

My understanding in regard to the OP is that it was a rather trivial
matter in which he was just trying to format the output for his
JavaScript practice.
Function parseFloat is not needed in that code.

Probably not, but it does demonstrate that the OP is aware of the
automatic type conversion and is trying to make sure that he is not
working with strings. You know perfectly well that it is a common
error for people to think they are performing addition when in fact
they are doing concatenation.
Please read tha faq and repair your manners or be prepared to be ploncked.

Evertjan, when people are nice to me, I'm nice back. When people are
assholes to me, I'm an asshole back. (It's in the bible.)

And I'm not sure what ploncked means. Is 'ploncked' in the FAQ?

I think I love you guys, byw. This usenet group has everything I'm
looking for in a usenet group. People with a lot of technical
knowledge in a subject of which I only marginally care about, and
people who like to take the occasional pisser.
 
E

Evertjan.

Doug Gunnoe wrote on 27 apr 2008 in comp.lang.javascript:
Evertjan, when people are nice to me, I'm nice back. When people are
assholes to me, I'm an asshole back. (It's in the bible.)

And I'm not sure what ploncked means. Is 'ploncked' in the FAQ?

No. it is general usenet slang.
Every now and then, the Germans say something worthwile,
a translation not being so to tthe point as the original:

"Das PLONCK! symbolisiert das Geräusch des Aufschlags im Killfile."

I think I love you guys, byw. This usenet group has everything I'm
looking for in a usenet group. People with a lot of technical
knowledge in a subject of which I only marginally care about, and
people who like to take the occasional pisser.

Don't take it hard, Doug, we are here to enjoy,
and Javascript, like chess, gives intellectual enjoyment.
 
D

Dr J R Stockton

In comp.lang.javascript message <4003c6a8-7bfa-448b-a541-7f1e1613d724@j2
2g2000hsf.googlegroups.com>, Sat, 26 Apr 2008 18:04:53, Doug Gunnoe
lol.

Remark away.


My understanding in regard to the OP is that it was a rather trivial
matter in which he was just trying to format the output for his
JavaScript practice.

Agreed. But you gave an answer inadequate for the application. You are
either one of the subtler trolls, or too inexperienced to be of any real
use.
Probably not, but it does demonstrate that the OP is aware of the
automatic type conversion and is trying to make sure that he is not
working with strings. You know perfectly well that it is a common
error for people to think they are performing addition when in fact
they are doing concatenation.

A pointless remark. Those who, in that case, use parseFloat have only
made the obvious first step in their understanding. You, it seems,
choose to go no further.
Evertjan, when people are nice to me, I'm nice back. When people are
assholes to me, I'm an asshole back.

Giving unsatisfactory answers when you could have left the matter to
those more capable is not nice; you get here what you therefore deserve.

And I'm not sure what ploncked means. Is 'ploncked' in the FAQ?

It is, no doubt, Dutch for "plonked". One who had read Usenet for a
reasonable time before posting would have discovered that word.
 
D

Doug Gunnoe

Agreed. But you gave an answer inadequate for the application.

It was perfectly 'adequate' for the application, considering the
application.
You are either one of the subtler trolls,

I've been called worse
or too inexperienced to be of any real use.

Use for whom? For someone as smart as yourself, with such deep, low
level technical knowledge of JavaScript, I doubt that I would be of
any benefit. That being the case, please feel free to ignore all my
future posts.
A pointless remark. Those who, in that case, use parseFloat have only
made the obvious first step in their understanding.

Which was the only point I was making. If you ever get around to
reading the OP, he states several times that he is a n00b to
JavaScript.
You, it seems, choose to go no further.

I had no need to go any further. Like me, you didn't notice it the
first time you browsed through it either. It was only after I
challenged your asshatery that you went back and looked for additional
ways to criticize my answer. (Oh, and you know it's true lol.)
Giving unsatisfactory answers when you could have left the matter to
those more capable is not nice; you get here what you therefore deserve.

If you will notice, Mr. Stockton, those more capable have still not
answered the OP's question.
Don't take it hard, Doug, we are here to enjoy,
and Javascript, like chess, gives intellectual enjoyment.

It's cool, Evertjan. I don't mind being wrong. If I did my life would
be total misery.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top