Solved

Round to Decimal Point problem

Posted on 2008-10-14
10
554 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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 

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

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

How to Win a Jar of Candy Corn: A Scientific Approach! I love mathematics. If you love mathematics also, you may enjoy this tip on how to use math to win your own jar of candy corn and to impress your friends. As I said, I love math, but I gu…
This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
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…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

744 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

12 Experts available now in Live!

Get 1:1 Help Now