0

I have a Java Android app that deals with tips. When I take in the double of bill amount and tip percent, I parse them to to strings to display later.

How can I easily format the doubles to make the currency more readable at the end? Instead of looking like $1.0 it would be $1.00.

The code I have mentioned is:

    final EditText amt = (EditText) findViewById(R.id.bill_amt);
    final EditText tip = (EditText) findViewById(R.id.bill_percent);
    final TextView result = (TextView) findViewById(R.id.res);
    final TextView total = (TextView) findViewById(R.id.total);

double amount = Double.parseDouble(amt.getText().toString());
double tip_per = Double.parseDouble(tip.getText().toString());
double tip_cal = (amount * tip_per) / 100;

double totalcost = amount + tip_cal;
result.setText("Tip Amount : " + " $ " + Double.toString(tip_cal));
total.setText("Total Cost: "  + " $ " + totalcost);

I would like result and total at the end to be the outputs that are nicely formatted doubles to 3 places. Thanks for any advice you can give.

JoxTraex
  • 13,423
  • 6
  • 32
  • 45
user1642370
  • 27
  • 1
  • 6

1 Answers1

0

you can user DecimalFormat to do the job for you

ex

double x = 1.0;
DecimalFormat decimalFormat = new DecimalFormat("$#0.00");
String text = decimalFormat.format(x);

[enter link description here][1]

http://docs.oracle.com/javase/7/docs/api/java/text/DecimalFormat.html

moh.sukhni
  • 2,220
  • 19
  • 24