Write a script that reads the coefficients a, b and c of a quadratic equation ax2 + bx + c = 0 and solves it (prints its real roots). Calculates and prints its real roots. Print numbers with two digits of precision after the floating point
Here is what I've created so far :
function solve(args) {
var a= +args[0];
var b= +args[1];
var c= +args[2];
var d = b*b - (4 * a * c);
var x1 = (-b + Math.sqrt(d)) / 2 * a;
var x2 = (-b - Math.sqrt(d)) / 2 * a;
if (0 > d) {
console.log("no real roots");
}
else if (d > 0) {
console.log("x1 = " + x1);
console.log("x2 = " + x2);
}
else {
x1 = -b /2*a;
console.log("x1 =" + x1);
}
}
My question is how to I print the numbers with two digits of precision and is my logic wrong somewhere? The language is JavaScript. I've searched in the existing questions, but the answers don't seem to work for me.
Thank you ! I don't want to round the number. I want precision. Example x1 =3.555 output x1 =3.55