Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Java by (9.5k points)

Below is my code implementation:

var someNumber = 123.456;

someNumber = someNumber.toFixed(2);

alert(typeof(someNumber));

//alerts string

The toFixed() method returns a string but I want to round the number to 2 decimal places. Can anyone tell me how to do it?

1 Answer

0 votes
by (19.7k points)

You get a string because 0.1 and powers are not representable in binary floating-point systems. So to trim it to the exact precision required for display, outputting it as a string is the only solution. 

Interested in Java? Check out this Java Certification by Intellipaat.   

Related questions

0 votes
1 answer
asked Apr 19, 2021 in Java by sheela_singh (9.5k points)
0 votes
1 answer
0 votes
1 answer
0 votes
1 answer

Browse Categories

...