Possible Duplicate:
USD Currency Formatting in Java
All- I have a small problem in which my app outputs an answer after inputs go through an equation and are rounded to two decimal places after the decimal point. My problem is that if the number comes out even like $12.80 the output looks like $12.8. I want it to look like $12.80. Not the biggest deal but I am in the final stages of development and would like to clean it up to look nice. Here is some of my code:
Float result2 = result / l3;
double data2 = result2;
int decimalPlaces2 = 2;
double roundBase2 = Math.pow(10, decimalPlaces2);
data2 = (double)(Math.round(data2 * roundBase2)) / roundBase2;
answer.setText("$" + Double.toString(data2));
Thanks for your time!
DecimalFormat format = new DecimalFormat("$#");
format.setMinimumFractionDigits(2);
answer.setText(format.format(data2));
Here's how I like to do it:
float a = 12.5;
String money = String.format("$%.2f", a);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With