Recently, while I make my Data Analyzer app, I ran into a problem: There is a deviation while calculating with float numbers. For example, 19.6 / 10 is not equal to 1.9600000000000002

I tried to search the Internet, but there are not enough solutions to solve my problem. Some solutions were not helpful with my program, they still have deviations.

I have strong computer programming ability, so I can solve dozens of programming problems patiently and successfully. View my skills

I kept calm, then I thought a solution by myself after an hour. Now, I am going to share my solution to solve your problem.

What is Calculate Deviation in JavaScript?

In JavaScript, calculating float numbers can cause deviations, the result is not correct.

This happens when adding, subtracting, multiplying, dividing or using Math.pow(a,b); in float numbers (not integers).

If both numbers are integers, the result will be accurate, even if the result is not an integer.

For example, 19.6 / 10 is not equal to 1.9600000000000002, because 19.6 is not an integer, even as 10 is an integer.

How did I solve this problem?

Calculating accurately is not easy, we need to use so many codes to make sure the results are accurate. So I put the codes into functions, you just need to use the function to calculate accurately.

I had an idea to solve it: Turn the float numbers to integers by multiplying 10, 100, 1000, etc. Not using parseInt(); Math.floor(); Math.round(); or Math.ceil(); functions. Then divide the result back.

I use a special way to multiply both numbers because multiplying 10 maybe also not accurate. It is adding several zeros as a string, then add to the numbers (it turns the numbers to strings) and use parseInt(); to turn they back to integers.

For example, there are two numbers a and b. We usually use “a / b” to divide, it is not accurate. My functions will look at the decimal digits of two numbers, and save the greater number c. If both a and b are integers, the function will calculate without any modifications. Multiply 10 for a and b if c=1, multiply 100 if c=2, multiply 1000 if c=3, etc. Then calculate a and b. And then divide the result back to origin and return the final result.

My Solution Functions Code

Last Updated: Jul. 27th, 2019 11:30 PM

Fixed a bug about some numbers still have deviations.

Copy and paste the code below to the top of your JavaScript script, you also can save this as a .js file and use it repeatedly.

// This function is used to prevent the deviation for adding
// 此方法用于防止加法误差
function accAdd(arg1, arg2) {
var zeros1 = "";
var zeros2 = "";
var xiaoShuWeiShu1 = 0;
var xiaoShuWeiShu2 = 0;
var arg11 = 0;
var arg21 = 0;
if((arg1 + "").includes(".")){
xiaoShuWeiShu1 = (arg1+"").split(".").length;
}else{
xiaoShuWeiShu1 = 0;
}
if((arg2 + "").includes(".")){
xiaoShuWeiShu2 = (arg2+"").split(".").length;
}else{
xiaoShuWeiShu2 = 0;
}
zeros1 = "";
if(xiaoShuWeiShu1 == 0 &amp;&amp; xiaoShuWeiShu2 == 0){
return arg1 + arg2;
}else{
arg1 = arg1 + "";
arg2 = arg2 + "";
arg1 = arg1.replace(".", "");
arg2 = arg2.replace(".", "");
if(xiaoShuWeiShu1 > xiaoShuWeiShu2){
for(var i631=0; i631<xiaoShuWeiShu1-xiaoShuWeiShu2; i631++){
zeros1 = zeros1 + "0";
}
for(var i631=0; i631<xiaoShuWeiShu1; i631++){
zeros2 = zeros2 + "0";
}
arg2 = arg2 + zeros1;
}else if(xiaoShuWeiShu1 < xiaoShuWeiShu2){
for(var i631=0; i631<xiaoShuWeiShu2-xiaoShuWeiShu1; i631++){
zeros1 = zeros1 + "0";
}
for(var i631=0; i631<xiaoShuWeiShu2; i631++){
zeros2 = zeros2 + "0";
}
arg1 = arg1 + zeros1;
}else{
for(var i631=0; i631<xiaoShuWeiShu1; i631++){
zeros1 = zeros1 + "0";
zeros2 = zeros2 + "0";
}
}
arg11 = parseInt(arg1);
arg21 = parseInt(arg2);

return (arg11 + arg21) / parseInt("1" + zeros2);
}
}

// This function is used to prevent the deviation for subtracting
// 此方法用于防止减法误差
function accSub(arg1, arg2) {
var zeros1 = "";
var zeros2 = "";
var xiaoShuWeiShu1 = 0;
var xiaoShuWeiShu2 = 0;
var arg11 = 0;
var arg21 = 0;
if((arg1 + "").includes(".")){
xiaoShuWeiShu1 = (arg1+"").split(".").length;
}else{
xiaoShuWeiShu1 = 0;
}
if((arg2 + "").includes(".")){
xiaoShuWeiShu2 = (arg2+"").split(".").length;
}else{
xiaoShuWeiShu2 = 0;
}
if(xiaoShuWeiShu1 == 0 &amp;&amp; xiaoShuWeiShu2 == 0){
return arg1 - arg2;
}else{
arg1 = arg1 + "";
arg2 = arg2 + "";
arg1 = arg1.replace(".", "");
arg2 = arg2.replace(".", "");
if(xiaoShuWeiShu1 > xiaoShuWeiShu2){
for(var i631=0; i631<xiaoShuWeiShu1-xiaoShuWeiShu2; i631++){
zeros1 = zeros1 + "0";
}
for(var i631=0; i631<xiaoShuWeiShu1; i631++){
zeros2 = zeros2 + "0";
}
arg2 = arg2 + zeros1;
}else if(xiaoShuWeiShu1 < xiaoShuWeiShu2){
for(var i631=0; i631<xiaoShuWeiShu2-xiaoShuWeiShu1; i631++){
zeros1 = zeros1 + "0";
}
for(var i631=0; i631<xiaoShuWeiShu2; i631++){
zeros2 = zeros2 + "0";
}
arg1 = arg1 + zeros1;
}else{
for(var i631=0; i631<xiaoShuWeiShu1; i631++){
zeros1 = zeros1 + "0";
}
}
arg11 = parseInt(arg1);
arg21 = parseInt(arg2);

return (arg11 - arg21) / parseInt("1" + zeros2);
}
}

// This function is used to prevent the deviation for multiplying
// 此方法用于防止乘法误差
function accMul(arg1, arg2) {
var zeros1 = "";
var xiaoShuWeiShu1 = 0;
var xiaoShuWeiShu2 = 0;
var arg11 = 0;
var arg21 = 0;
if((arg1 + "").includes(".")){
xiaoShuWeiShu1 = (arg1+"").split(".").length;
}else{
xiaoShuWeiShu1 = 0;
}
if((arg2 + "").includes(".")){
xiaoShuWeiShu2 = (arg2+"").split(".").length;
}else{
xiaoShuWeiShu2 = 0;
}
if(arg2 == 0){
return 0;
}else if(arg2 == 1){
return arg1;
}else if(xiaoShuWeiShu1 == 0 &amp;&amp; xiaoShuWeiShu2 == 0){
return arg1 * arg2;
}else{
arg1 = arg1 + "";
arg2 = arg2 + "";
arg1 = arg1.replace(".", "");
arg2 = arg2.replace(".", "");
if(xiaoShuWeiShu1 > xiaoShuWeiShu2){
for(var i631=0; i631<xiaoShuWeiShu1-xiaoShuWeiShu2; i631++){
zeros1 = zeros1 + "0";
}
arg2 = arg2 + zeros1;
}else if(xiaoShuWeiShu1 < xiaoShuWeiShu2){
for(var i631=0; i631<xiaoShuWeiShu2-xiaoShuWeiShu1; i631++){
zeros1 = zeros1 + "0";
}
arg1 = arg1 + zeros1;
}else{
for(var i631=0; i631<xiaoShuWeiShu1; i631++){
zeros1 = zeros1 + "0";
}
}
arg11 = parseInt(arg1);
arg21 = parseInt(arg2);

return arg11 * arg21 / parseInt("1" + zeros1 + zeros1);
}
}

// This function is used to prevent the deviation for dividing
// 此方法用于防止除法误差
function accDiv(arg1, arg2) {
var zeros1 = "";
var xiaoShuWeiShu1 = 0;
var xiaoShuWeiShu2 = 0;
var arg11 = 0;
var arg21 = 0;
if((arg1 + "").includes(".")){
xiaoShuWeiShu1 = (arg1+"").split(".").length;
}else{
xiaoShuWeiShu1 = 0;
}
if((arg2 + "").includes(".")){
xiaoShuWeiShu2 = (arg2+"").split(".").length;
}else{
xiaoShuWeiShu2 = 0;
}
if(arg2 == 0){
if(arg1 > 0){
return Infinity;
}else if(arg < 0){
return -Infinity;
}else{
return NaN;
}
}else if(arg2 == 1){
return arg1;
}else if(xiaoShuWeiShu1 == 0 &amp;&amp; xiaoShuWeiShu2 == 0){
return arg1 / arg2;
}else{
arg1 = arg1 + "";
arg2 = arg2 + "";
arg1 = arg1.replace(".", "");
arg2 = arg2.replace(".", "");
if(xiaoShuWeiShu1 > xiaoShuWeiShu2){
for(var i631=0; i631<xiaoShuWeiShu1-xiaoShuWeiShu2; i631++){
zeros1 = zeros1 + "0";
}
arg2 = arg2 + zeros1;
}else if(xiaoShuWeiShu1 < xiaoShuWeiShu2){
for(var i631=0; i631<xiaoShuWeiShu2-xiaoShuWeiShu1; i631++){
zeros1 = zeros1 + "0";
}
arg1 = arg1 + zeros1;
}else{
for(var i631=0; i631<xiaoShuWeiShu1; i631++){
zeros1 = zeros1 + "0";
}
}
arg11 = parseInt(arg1);
arg21 = parseInt(arg2);

return arg11 / arg21;
}
}

How to use my solutions to calculate accurately?

Use function accAdd(a, b); , accSub(a, b); , accMul(a, b); or accDiv(a, b); , replace a and b to your number.

// 准确地计算加法
var addResult = accAdd(a, b); // a + b
// Subtract Accurately
// 准确地计算减法
var subtractResult = accSub(a, b); // a - b
// Multiply Accurately
// 准确地计算乘法
var multiplyResult = accMul(a, b); // a * b
// Divide Accurately
// 准确地计算除法
var divideResult = accMul(a, b); // a / b

For Example, 19.6 divides 10

// Add my solution code above here first.
// 先在此处添加上面的解决方案代码
var a = 19.6;
var b = 10;
var c = accDiv(a, b);
alert(a + " divides " + b + " is equal to " + c);

If you have programming problems, let me know

I have strong computer programming ability. If you can’t solve your programming problems of JavaScript by yourself like this problem, you can let me know. I will solve your problem as I can.

You now can use programming languages to communicate with me. Programming languages are allowed while contacting me or sending messages to me.

Conclusion

This code is my idea, you can use it for free. I hope my solution code can be useful for you to solve your problem.

I am a Chinese student, this is my personal website and blog. I have strong programming ability, but this can’t be meant there is no bug in my programs. If you have suggestions or found any bugs in this code, comment your suggestions below or contact me or report bugs of my programs anytime, I will answer it soon.

Thanks so much for reading!

This site uses Akismet to reduce spam. Learn how your comment data is processed.