Rounded up the values obtained

Asked

Viewed 73 times

-2

Good afternoon, I am preparing, where the matrix is formed by the following code:

var valores4='';
for(var LL =0;LL < noc*nol;LL++){
    for(var CC =0;CC < nol*noc;CC++){
        if(CC < noc*nol-1){
            valores4 += matriz_valores[LL][CC] + "\t";  
        }
        else{
            valores4 += matriz_valores[LL][CC] + "\n"; 
        }       

        var valores5 = '';
        for(var C1=0;C1<noc*noc;C1++){
          valores5 += matriz_cte[C1] + "\n";    
        }   


        console.log("Matriz com o Triângulo Inferior = 0: \n", valores4);
        console.log("Vetor [b] atualizados: \n", valores5);
    }
}

He is working, but the answers obtained are:

Matriz com o Triângulo Inferior = 0: 
 -1.975 0.263   0.250   0.000
0   -1.0316232911392405 0.017468354430379748    0.25
0   0   -1.4831279301246258 0.27348795654973657
0   0   0   -0.8578988360199865

How can I rent the decimal places???

1 answer

1


In Javascript to round values you have 3 functions: Math.round, Math.Ceil and Math.floor...

But if its value assumes some string character, the value returned by these functions will be an Nan(Not a Number), it means that it tried to cast (conversion of types) and even then it was not possible to generate a number, resulting in an Nan...

To solve this you must make sure that the value type is Number before using the function, for this you can convert in two ways:

  • Using the Number constructor:

    const digitosEmString = '51020';
    
    const digitosEmNumber = Number(digitosEmString);
    
  • Using the "+" operator in front of the string:

    const digitosEmNumber = +digitosEmString;
    
  • I could do it here, I only had to make this change in my code: values4 += Number(matriz_values[LL][CC]). toFixed(3) +" t"; E reduced decimal places. .

Browser other questions tagged

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