'How to keep two decimals even if the second decimal is a zero?

So I use ...

System.out.printf("%.2f",variable);

and it works. But if the second decimal place is a 0, it just ignores the zero and gives the answer to one decimal place. How can I get two decimal places including the zero?

Thanks.



Solution 1:[1]

you can use DecimalFormat like this

DecimalFormat decimalFormat = new DecimalFormat("#0.00");
String format = decimalFormat.format(variable);

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 mahdi sharbaty