How to keep decimal precision when multiplying and dividing (in jQuery, javascript)?

I have the following function that can do multiplication and division for some business logic in jQuery

var rate = 39.5;
var IndiviualPercentageTaxes = 20;
var TotalPercentageTaxes = 20;

var r1 = rate* 100 / (100 + IndiviualPercentageTaxes);  

var r2 = r1* (100 + TotalPercentageTaxes)/100;

var finalRate = Math.round(r2));

FinalRate 39 , but the expected result is 40 .....

I can easily rewrite the program to avoid losing decimal precision in the calculation, for example:

  var r2 = rate*(100 + TotalPercentageTaxes)/(100 +TotalPercentageTaxes)

However, this will skip r1, and I need r1 for other purposes throughout the program.

It also caused confusion that no one knows what this formula means, since I combine two calculations in one.

Are there any suggestions?

+4
source share
1 answer

? , , , .

, . . : https://github.com/MikeMcl/bignumber.js/, , .

" " - Common Lisp Java, , ( ) "" (.. ), , .

+1

Source: https://habr.com/ru/post/1610313/


All Articles