Solved

Round to Decimal Point problem

Posted on 2008-10-14
10
556 Views
Last Modified: 2012-05-05
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
0
Comment
Question by:paulleeson
  • 5
  • 3
10 Comments
 
LVL 3

Expert Comment

by:DerkArts
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

by:DerkArts
ID: 22709655
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

0
 

Author Comment

by:paulleeson
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

by:
deighton earned 100 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
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 

Author Comment

by:paulleeson
ID: 22709907
Hi Deighton,

Spot on and thanks for explaining. All makes sense.

Thanks also to DerkArts.

Paul
0
 
LVL 3

Expert Comment

by:DerkArts
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

by:paulleeson
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

by:paulleeson
ID: 22718937
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.
0
 

Author Comment

by:paulleeson
ID: 22719392
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.
0

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

This article seeks to propel the full implementation of geothermal power plants in Mexico as a renewable energy source.
This article will show, step by step, how to integrate R code into a R Sweave document
Learn the basics of strings in Python: declaration, operations, indices, and slicing. Strings are declared with quotations; for example: s = "string": Strings are immutable.: Strings may be concatenated or multiplied using the addition and multiplic…
The viewer will learn how to count occurrences of each item in an array.

862 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

24 Experts available now in Live!

Get 1:1 Help Now