Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

248
Views
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
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!