How to print a double with two decimals in Android? [duplicate] How to print a double with two decimals in Android? [duplicate] android android

How to print a double with two decimals in Android? [duplicate]


yourTextView.setText(String.format("Value of a: %.2f", a));


For Displaying digit upto two decimal places there are two possibilities - 1) Firstly, you only want to display decimal digits if it's there.For example - i) 12.10 to be displayed as 12.1, ii) 12.00 to be displayed as 12. Then use-

DecimalFormat formater = new DecimalFormat("#.##"); 

2) Secondly, you want to display decimal digits irrespective of decimal present For example -i) 12.10 to be displayed as 12.10. ii) 12 to be displayed as 12.00.Then use-

DecimalFormat formater = new DecimalFormat("0.00");