Calculation Error in JavaScript

0

In a calculation made in javaScript I am having the following problem, I have tried to solve in several ways without success, I need to do a simple math operation and in javascript it gives a difference of 1 cent.

The calculation would be = (268.25 - 5.36 + 1.01) The expected result is 263.90.

But I need to pass on this result a function that formats with 2 decimal places without rounding or modifying the data, for example, if the value for 2.9999999 function would have to return 2.99, but the above result would not be a rounding error since the even returns the exact number.

To do this formatting I use the function below:

function toFixed(num, fixed) {
    fixed = fixed || 0;
    fixed = Math.pow(10, fixed);
    return Math.floor(num * fixed) / fixed;
}

When the total is passed by it the value returned is 263.89, generating that 1 cent difference.

    
asked by anonymous 31.07.2018 / 16:39

2 answers

1

The .toFixed might not solve the problem , because in some cases it will "round" some values and what you seem to need is to trim the number, so what you need is to treat as a string

Possibly your goal is to add a zero to the "front" as well as trim the decimal places, as I believe you intend to display this for the user, so an example would not round :

var x = 268.25,
    y = 5.36,
    z = 1.01;

console.log("Calculo:", x - y + z);

var para_exibir = String(x - y + z) + "0";

console.log("Para o usuário/imprimir:", para_exibir);

Of course, if the value is something like 2.9999 it will have to trim, so you can use a function to check if the decimal size is greater than "a house" and thus trim or complete the string, eg

function formatar(f, precisao) {
   f = String(f);

   var i = f.indexOf(".");

   if (i > -1) {
       var d = f.substr(i + 1),    //Pega somente as casas decimais
           n = f.substr(0, i + 1); //Pega o valor "inteiro"

       if (d.length < precisao) { // Se for menor que duas casas adiciona o zero
          f = n + d + ("0".repeat(precisao - 1)); /*o -1 é porque neste caso já deve ter um digito na casa, então só adiciona os zeros que faltam*/
       } else if (d.length > precisao) { // Se for maior que duas casas apara a string sem arredondar o valor
          f = n + f.substr(i + 1, precisao);
       }
   }

   return f;
}

The usage is:

formatar ( int valor , int precisao )

Example:

var x = 268.25,
    y = 5.36,
    z = 1.01;


function formatar(f, precisao) {
    f = String(f);

    var i = f.indexOf(".");

    if (i > -1) {
        var d = f.substr(i + 1),
            n = f.substr(0, i + 1);

        if (d.length < precisao) {
            f = n + d + ("0".repeat(precisao - 1));
        } else if (d.length > precisao) {
            f = n + f.substr(i + 1, precisao);
        }
    }

    return f;
}

//Com "precisão" de duas casas
console.log("10.9999 formatado:", formatar(10.9999, 2) );
console.log("10.9 formatado:", formatar(10.9, 2) );
console.log("10.99 formatado:", formatar(10.99, 2) );
console.log("x - y + z formatado:", formatar(x - y + z, 2) );

console.log("-----------");

//Com "precisão" de três casas
console.log("10.4445 formatado:", formatar(10.4445, 3) );
console.log("10.4 formatado:", formatar(10.4, 3) );
console.log("10.455 formatado:", formatar(10.455, 3) );
console.log("x - y + z formatado:", formatar(x - y + z, 3) );
    
31.07.2018 / 17:15
0
___ erkimt ___ Calculation Error in JavaScript ______ qstntxt ___

In a calculation made in javaScript I am having the following problem, I have tried to solve in several ways without success, I need to do a simple math operation and in javascript it gives a difference of 1 cent.

The calculation would be = (268.25 - 5.36 + 1.01) The expected result is 263.90.

But I need to pass on this result a function that formats with 2 decimal places without rounding or modifying the data, for example, if the value for 2.9999999 function would have to return 2.99, but the above result would not be a rounding error since the even returns the exact number.

To do this formatting I use the function below:

function numberFormatPrecision(number, format) {   
    var number = number.toFixed(3);
    return number.substring(0, number.length - 1);
}

When the total is passed by it the value returned is 263.89, generating that 1 cent difference.

    
______ azszpr319007 ___

The %code% might not solve the problem , because in some cases it will "round" some values and what you seem to need is to trim the number, so what you need is to treat as a string

Possibly your goal is to add a zero to the "front" as well as trim the decimal places, as I believe you intend to display this for the user, so an example would not round :

function numberFormatPrecision(number, format) {   
    var number = number.toFixed(3);
    return number.substring(0, number.length - 1);
}

Of course, if the value is something like %code% it will have to trim, so you can use a function to check if the decimal size is greater than "a house" and thus trim or complete the string, eg

%pre%

The usage is:

%pre%

Example:

%pre%
    
______ ___ azszpr319058

The way it worked for what I needed was this:

%pre%     
___
31.07.2018 / 19:29