为什么我的输出没有显示我想要的正确

时间:2015-09-24 01:47:52

标签: java

import java.util.Scanner;
public class CalculatePurchasePrice
{

  public static void main(String [] args)
  {


    Book Cost: $10.00
    State Tax Charged: $0.80
    County Tax Charged: $0.30
    Total Purchase Price: $11.10


    Scanner kbd = new Scanner (System.in);

    System.out.println("Price of the book: ");

    bookPrice = kbd.nextDouble(); // get user input

    double staxCharged = bookPrice * stateTax;

    System.out.println("State Tax Charged: " + staxCharged);

    double ctaxCharged = bookPrice * countyTax;

    System.out.println("County Tax Charged: " + ctaxCharged);

    totalPP = bookPrice + staxCharged + ctaxCharged;

    System.out.println("Total Purchase Price: " + totalPP);

   }
}

总计假设显示$ 11.10
总计显示$ 11.1000000000001

1 个答案:

答案 0 :(得分:1)

这应该这样做:System.out.printf("总购买价格:%。2f%n",totalPP);

您应该阅读https://docs.oracle.com/javase/tutorial/java/data/numberformat.html