Link to home
Start Free TrialLog in
Avatar of paulleeson
paulleeson

asked on

Round to Decimal Point problem

Hi, I am very new to javascript and I am using various resources on the web to teach myself.

I am trying to put together a price calulator which I have working but cannot get the totals to 2 decimals points. I have based the calculator on http://javascript.internet.com/math-related/checkbox-calculator.html

My version calulates a daily price and also the total weekly price once the calculate button is pressed.

There may be much better ways to do it as this is an old tutoria.l In any case I have tried a few things such as

document.calc.monpay.value=Math.round(witem1 + witem2 + witem3 + witem4 + witem5 + witem6 + witem7 + witem8*100)/100;

This would for example produce a leading zero i.e 0.54 rather than 54.05. If I use *1)/1; then this would produce 54.

If I do not use the Math.Round than I get 2 decimal places on occasion but the above would for example be 54.05000000000.

I hope this makes sense. Please advise if I am going about this in the wrong way.

I have attached the calculate.js. Please remember I am learning and I should imagine this is very bloated.


Copy-of-calculate.zip
Avatar of DerkArts
DerkArts
Flag of Netherlands image

I think think this is what you need...

num == (witem1 + witem2 + witem3 + witem4 + witem5 + witem6 + witem7 + witem8*100)/100;
document.calc.monpay.value =  num.toFixed(2);
Excuse me, small typo:
num = (witem1 + witem2 + witem3 + witem4 + witem5 + witem6 + witem7 + witem8*100)/100;
document.calc.monpay.value =  num.toFixed(2);

Open in new window

Avatar of paulleeson
paulleeson

ASKER

Hi DerkArts,

Still get a funny one with that.

i.e when selecting the checkboxes to add:
var mon1price = 11.06;
var mon2price = 11.06;
var mon3price = 11.06;
var mon4price = 11.06;
var mon5price = 9.81;

I get 0.54 as the total. It should be 54.05.

Paul

ASKER CERTIFIED SOLUTION
Avatar of deighton
deighton
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Hi Deighton,

Spot on and thanks for explaining. All makes sense.

Thanks also to DerkArts.

Paul
I'm sorry, i havent really looked at you formula, just provided a way you get to two decimal places. I'm not sure why you would want to devide by 100, the above example is probably correct, eg:

num = (witem1 + witem2 + witem3 + witem4 + witem5 + witem6 + witem7 + witem8)
document.calc.monpay.value =  num.toFixed(2);

Altough you would not need 'toFixed(2)'  any longer, because this formula will never result in more than two decimal places (with the amounts you listed above)
Hi DerkArts

Just tried without the 'tofixed(2)'

Got an issue with the answer being 54.050000000000004

The addition of the 'tofixed(2)' corrects this to 54.05.

I chose the selected answer as it was the one that worked for me.
Both DerkArts answers, whilst very helpful I am afraid did not give me the final answer.
I can prove that Deightons answer worked with a working example link if required.
I would also like to add.

This is Deightons Answer
num =witem1 + witem2 + witem3 + witem4 + witem5 + witem6 + witem7 + witem8;
document.calc.monpay.value =  num.toFixed(2);

This was DerkArts
num = (witem1 + witem2 + witem3 + witem4 + witem5 + witem6 + witem7 + witem8*100)/100;
document.calc.monpay.value =  num.toFixed(2);

I cannot see how the difference can be seen as a typo.

Experts Exchange is also here to help people. If someone does happen to spot a mistake and therefore corrects this then they surley deserve the points for providing the final working answer.