Java - Data Format Format decimal

Description

Format decimal

Demo

import java.text.DecimalFormat;
import java.util.Scanner;

public class BillCalculator {

  public static void main(String args[]) {
    double mealCharge;

    Scanner keyboard = new Scanner(System.in);
    System.out.println("Please enter the charge for the meal.");
    mealCharge = keyboard.nextDouble();/*from  w ww .ja v  a 2s .c  o m*/
    keyboard.close();

    double tax = .0675 * mealCharge;
    double tip = .15 * (mealCharge + tax);
    double bill = mealCharge + tax + tip;

    DecimalFormat formatter = new DecimalFormat("$#,##0.00");

    System.out.println("Meal Charge = " + formatter.format(mealCharge));
    System.out.println("Tax = " + formatter.format(tax));
    System.out.println("Tip = " + formatter.format(tip));
    System.out.println("Bill = " + formatter.format(bill));
  }
}

Related Topic