Solved

My JavaScript Does Not Calculate Correctly

Posted on 2008-10-29
12
173 Views
Last Modified: 2012-08-13
I just had some help on this script a few weeks ago and just noticed it does not calculate correctly.  It worked correctly with $100.00 input but not at $30.00 input....

You can view it here: http://billingpros.com/paypal_payment.htm

If you put a $30.00 amount in you will see it return $29.04 and it should be $28.83
<script language="JavaScript">
 

function calc(val) {

  var num = parseFloat(val.replace('$',''));

  if (isNaN(num)) return 0;

  var fee1 = (num/100)*2.9; // 2.9%

  var fee2 = (num/100)*0.30; // .30

  var newNum = num-(fee1+fee2)

  return "$"+(newNum.toFixed(2))

}

 </script>

Open in new window

0
Comment
Question by:adspace
  • 5
  • 3
  • 2
  • +2
12 Comments
 
LVL 13

Expert Comment

by:Xyptilon2
ID: 22838156
If I compute it manually, I'm also getting 29.04, why should it come out as 28.83?

0.3 * 2.9 = 0.87
0.3 * 0.3 = 0.09
Total = 0.96

30 - 0.96 = 29.04


0
 
LVL 44

Expert Comment

by:scrathcyboy
ID: 22838177
function calc(val) {
  var num = parseFloat(val.replace('$',''));
  if (isNaN(num)) return 0;
  num = num*100;
  var fee1 = num*29;
  var fee2 = num*3;
  var fees = fee1+fee2;
  var newNum = (num-fees)/1000;
  return "$"+(newNum.toFixed(2))

A little longer, but easy to figure out without decimals for someone not used to calculating floating points
0
 
LVL 16

Expert Comment

by:sh0e
ID: 22838185
30.00 - (.87 + .30) = 28.88
Why the second value should come out to .30 I don't know.  Maybe human error?
0
 

Author Comment

by:adspace
ID: 22838187
scrathcyboy
Have you tried that script...?
0
 
LVL 16

Expert Comment

by:sh0e
ID: 22838194
I get it now.  The original solution was a misunderstanding.  HE just wanted 0.30 deducted.
<script language="JavaScript">

 

function calc(val) {

  var num = parseFloat(val.replace('$',''));

  if (isNaN(num)) return 0;

  var fee1 = (num/100)*2.9; // 2.9%

  var fee2 = 0.30; // .30

  var newNum = num-(fee1+fee2)

  return "$"+(newNum.toFixed(2))

}

 </script>

Open in new window

0
 
LVL 16

Accepted Solution

by:
Lolly-Ink earned 250 total points
ID: 22838198
I think this is what you meant to do:
The reason why it was working for $100 is obvious now.

<script language="JavaScript">

 

function calc(val) {

  var num = parseFloat(val.replace('$',''));

  if (isNaN(num)) return 0;

  var fee1 = (num/100)*2.9; // 2.9%

  var fee2 = 0.30; // .30

  var newNum = num-(fee1+fee2)

  return "$"+(newNum.toFixed(2))

}
 

</script>

Open in new window

0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

Author Comment

by:adspace
ID: 22838203
Xyptilon2:
No, it should be 28.83
as its  (30 cents minus 2.9 percent (thats the formula)) and it does come out to 28.83
0
 
LVL 13

Expert Comment

by:Xyptilon2
ID: 22838231
Then the implementation of your algorithm is not right, your script does what it is told to do and returns 29.04 (fill in the numbers as below)

var fee1 = (num/100)*2.9; // 2.9%
(30/100)*2.9 = 0.87

var fee2 = (num/100)*0.30;
(30/100)*0.30 = 0.09

var newNum = num-(fee1+fee2)
30-(0.87+0.09) = 29.04

I'm guessing Lolly-ink is right and you want to implement that algorithm like that.

0
 

Author Comment

by:adspace
ID: 22838262
I am trying it now
0
 

Author Comment

by:adspace
ID: 22838321
Lolly-Ink:
That worked fine I took a while because I had to check several inputs i.e $44.52, 188.31 etc while using a calculator and its all good

Thanks a Bunch
Sam
0
 

Author Closing Comment

by:adspace
ID: 31511551
Thanks for your help
0
 
LVL 13

Expert Comment

by:Xyptilon2
ID: 22838353
Perhaps the next time you can rephrase your question so it more accurately describes what you want. The question you posted was not a problem in itself, the script returned the proper values. The calculations you used in the script itself were wrong, next time, please post the algorithm of what you want to accomplish so it is easier for us to understand the issue.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

In Part 1 (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/A_7849-Hex-Maze.html) we covered the hexagonal maze basics -- how the cells are represented in a JavaScript array and how the maze is displayed.  In this part, we'…
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. 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.: (CODE)
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…

867 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

18 Experts available now in Live!

Get 1:1 Help Now