Back
when converting from a float to a string, how can I get just 2 digits after the decimal point? For example, 5.56 instead of 5.56789.
You can use toFixed() function to get the required number of digits after the “.”. Here is an example of this:
function myFunction() { var num = 5.56789; var n = num.toFixed(2); console.log(n);}
function myFunction() {
var num = 5.56789;
var n = num.toFixed(2);
console.log(n);
}
This would give you an output: 5.56
Interested in full stack development? Check out the full stack developer course from Intellipaat.
31k questions
32.8k answers
501 comments
693 users