Solved

Round to Decimal Point problem

Posted on 2008-10-14
10
570 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
MIM Survival Guide for Service Desk Managers

Major incidents can send mastered service desk processes into disorder. Systems and tools produce the data needed to resolve these incidents, but your challenge is getting that information to the right people fast. Check out the Survival Guide and begin bringing order to chaos.

 
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
 

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

Learn by Doing. Anytime. Anywhere.

Do you like to learn by doing?
Our labs and exercises give you the chance to do just that: Learn by performing actions on real environments.

Hands-on, scenario-based labs give you experience on real environments provided by us so you don't have to worry about breaking anything.

Question has a verified solution.

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

We are taking giant steps in technological advances in the field of wireless telephony. At just 10 years since the advent of smartphones, it is crucial to examine the benefits and disadvantages that have been report to us.
JavaScript can be used in a browser to change parts of a webpage dynamically. It begins with the following pattern: If condition W is true, do thing X to target Y after event Z. Below are some tips and tricks to help you get started with JavaScript …
Learn the basics of while and for loops in Python.  while loops are used for testing while, or until, a condition is met: The structure of a while loop is as follows:     while <condition>:         do something         repeate: The break statement m…
The viewer will learn how to dynamically set the form action using jQuery.

734 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