jmasterx jmasterx - 3 months ago 21
Java Question

printf %f with only 2 numbers after the decimal point?

In my printf, I need to use %f but I'm not sure how to truncate to 2 deminal places:

ex:
getting

3.14159

to print as:

3.14

Thanks

Answer

Use this:

printf ("%.2f", 3.14159);