Go Premium for a chance to win a PS4. Enter to Win

x
Solved

# Round to Decimal Point problem

Posted on 2008-10-14
Medium Priority
583 Views
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 have attached the calculate.js. Please remember I am learning and I should imagine this is very bloated.

Copy-of-calculate.zip
0
Question by:paulleeson
• 5
• 3

LVL 3

Expert Comment

ID: 22709651
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);
0

LVL 3

Expert Comment

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

Author Comment

ID: 22709811
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

0

LVL 18

Accepted Solution

deighton earned 400 total points
ID: 22709886
don't forget that multiplication takes place before addition, so you were multiplying item8 by 100 only, then adding that to the other 7 values, then dividing by 100 as the last step (owing to the brackets).

try

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

Author Comment

ID: 22709907
Hi Deighton,

Spot on and thanks for explaining. All makes sense.

Thanks also to DerkArts.

Paul
0

LVL 3

Expert Comment

ID: 22709912
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)
0

Author Comment

ID: 22709949
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.

0

Author Comment

ID: 22718937
I chose the selected answer as it was the one that worked for me.
I can prove that Deightons answer worked with a working example link if required.
0

Author Comment

ID: 22719392
I would also like to add.

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

## Featured Post

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The Windows functions GetTickCount and timeGetTime retrieve the number of milliseconds since the system was started. However, the value is stored in a DWORD, which means that it wraps around to zero every 49.7 days. This article shows how to solve t…
Originally, this post was published on Monitis Blog, you can check it here . In business circles, we sometimes hear that today is the “age of the customer.” And so it is. Thanks to the enormous advances over the past few years in consumer techno…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…
###### Suggested Courses
Course of the Month13 days, 1 hour left to enroll