-2

I'm doing maintenance on this project the code don't have documentation and is hard. so I'm going with the problem. I'm fetching from the database .115 and when I'm doing the calculation for tax purpose I know the result from 1.00 * .115 is rounded to .12

So the Sub Total: 1.00 Tax: .012 Total: 1.11

I don't know how this happen.

This is my code for javascript

subT += pPrice * $('#qtySearch').val();
tax += pPrice * $('#qtySearch').val() * pTax;
total = tax + subT;

I tried Math.round and toFixed() to get this fixed but i don't have any good results

Any help will be appreciated.

Domino
  • 6,314
  • 1
  • 32
  • 58
  • 4
    It is not clear what the values of the variables are – H77 Jun 18 '17 at 05:17
  • `.val()` will always return a string – Jaromanda X Jun 18 '17 at 05:34
  • Rounding to decimals is a common problem in JavaScript. Beside the problem Jaromanda already mentioned you should look for [astorije's solution here](https://stackoverflow.com/a/21323330/4571082). – gus27 Jun 18 '17 at 05:40
  • Possible duplicate of [Formatting a number with exactly two decimals in JavaScript](https://stackoverflow.com/questions/1726630/formatting-a-number-with-exactly-two-decimals-in-javascript) – gus27 Jun 18 '17 at 05:40

1 Answers1

0

I think the first most important thing is to convert the value using parsefloat function then try to use ceil or floor function depends upon your need.

Rajat Jaiswal
  • 645
  • 4
  • 15