Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

259
Vistas
how to truncate output values in Nerdamer

I am using nerdamer.solve to solve roots but the roots are long and not truncated. I wanted to get truncated values upto 4 decimal places. How can I achieve this? I am using the following code to solve and display output in html:

var r = nerdamer.solve(`1 - ${a} * x^(${p}) + ${b}`, 'x');
document.getElementById("polesAns").innerHTML= r.toString();

The folllowing is output:

[(138655807/135201312)*i+49385501/48155102,(-138655807/135201312)*i+49385501/48155102,(58886197/57419096)*i-49385501/48155102,(-58886197/57419096)*i-49385501/48155102,-560373381/386371730,172668482/119053157,(145619303/100403024)*i-5753750945848186/10000000000000000000000000000000,(-560373381/386371730)*i-5753750945848186/10000000000000000000000000000000]

There is no division performed also.

I tried the solution posted here: How to use .toFixed() (or any alternative) on Nerdamer.solve solutions?

But how can I do this with my code? I tried the following:

    var value = `1 - ${a} * x^(${p}) + ${b}`;
 
 var toFixed = function(value, n) {
var img = Number(nerdamer.imagpart(value).text()).toFixed(n);
var real = Number(nerdamer.realpart(value).text()).toFixed(n);

// Format the number assuming i denotes imaginary in your case
var formatted = '';

if(real !== '0.0000') {
    formatted += real;
}

if(img !== '0.0000') {
    // Put the plus sign betweent the real and imaginary
    if(img.charAt(0) !== '-' && formatted) {
        formatted += '+';
    }
    // Assuming you're using i and not j for instance
    formatted += img+'i';
}

return formatted;
 };
 
 sol_raw = this.nerdamer.solve(value,'s');
 xs = this.nerdamer(sol_raw.toString()).each(function(solution) {
 roundedSolutions.push(toFixed(solution, 4));
    });

this.setState({
        solution: roundedSolution.join(''),
        equation:value})
     
  document.getElementById("polesAns").value = solution.toString();

I don't understand the this.setState() part , should i declare sol_raw and xs as var?

Also the substitution of variable is used in the my above root equation from advice here javascript Solving equation with subsitution of variable value

thank you

about 4 years ago · Juan Pablo Isaza
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda