Error Calculation in Javascript

Asked

Viewed 273 times

0

in a calculation done in Java I am having the following problem, I have tried to solve several ways without success, I need to do a simple mathematical operation and in javascript gives a difference of 1 cent.

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

however I need to pass in this result a function that format with 2 decimal places without rounding or modifying the data, for example, if the value by 2.9999999 function would have to return 2.99, however the above result would not be rounding error since it 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 through it the returned value is 263.89, generating this difference of 1 cent.

  • Have you tried using the javascript native toFixed function?

  • What are the values you pass on num and fixed?

  • 2

    The native "toFIxed" function does not solve your problem?

2 answers

1

The .toFixed may 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 treat as string

Possibly your the goal is to add a zero to "front" too, in addition to trimming the decimal places, as I believe you intend to display this to the user, then an example that 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 will have to trim, so you can use a function to check if the decimal size is larger than "a house" and so trim or complete the string, for example:

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 use 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) );

0

The way it worked for what I needed was this:

function numberFormatPrecision(number, format) {   
    var number = number.toFixed(3);
    return number.substring(0, number.length - 1);
}
  • What’s left to explain is why and how it works, right? If it doesn’t look supportive and people don’t understand the logic, because often logic can be used for countless other cases, as someone who needs 2.9998888 turns 2.9999, 3-house.

  • In a software we had the need to work with a number with two decimal places without it being rounded, briefly it formats with one more house and then uses substring to remove the last character, so a value of 5.996 would not become 6 but 5.99.

  • 2

    Dear Victor, please put this in the body of the answer, your response can help future visitors, understand that the site here is not like a forum of supports, it is something that we try to make profitable for all, your doubt today may be the solution for a future person with the same problem. Please add the technical explanation of what happens in the code and explain why it works.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.