Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add 0 to end if number is like $12.5 [duplicate]

Tags:

java

android

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!

like image 513
ninge Avatar asked Dec 10 '25 13:12

ninge


2 Answers

DecimalFormat format = new DecimalFormat("$#");
format.setMinimumFractionDigits(2);
answer.setText(format.format(data2));
like image 58
Cristian Avatar answered Dec 13 '25 04:12

Cristian


Here's how I like to do it:

float a = 12.5;

String money = String.format("$%.2f", a);
like image 34
Jack Satriano Avatar answered Dec 13 '25 05:12

Jack Satriano



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!