简体   繁体   中英

How to Round Decimals to 2 Places after the Decimal Point (Java)

I am fairly new to java and I have to create this program to which I have no idea where to start. Can someone please help me with what to do and how to write the code to get started?

Write a program that will emulate a cash register. Prompt the user to input the price of three items. Add them together to get a subtotal. Determine the tax (6% ) on the subtotal. Find the total amount of the sale subtotal plus tax. Display the price of each item, subtotal amount, tax amount and final amount.

So far I have this:

package register;
import java.util.Scanner;

public class Register {

    public static void main(String[] args) {

        Scanner price = new Scanner(System.in);

        System.out.print("Please enter a price for item uno $");
        double priceuno = price.nextDouble();

        System.out.print("Please enter a price for item dos $" );
        double pricedos = price.nextDouble();

        System.out.print("Please enter a price for item tres $");
        double pricetres = price.nextDouble();

        double total = ((priceuno) + (pricedos) + (pricetres));
        System.out.println("The subtotal is $" + total);

        double tax = .06;

        double totalwotax = (total * tax );
        System.out.println("The tax for the subtotal is $" + totalwotax);
        double totalandtax = (total + totalwotax);
        System.out.println("The total for your bill with tax is $" + totalandtax);

    }
}

The output (if the price is let's say price1 = 1.65, price2 = 2.82 and price3 = $9.08) looks like this:

Please anter a price for item number one $1.65

Please enter a price for item number two $2.82

Please enter a price for item number three $9.08

The subtotal is $13.55

The tax for the subtotal is $0.8130000000000001

The total for your bill with tax is $14.363000000000001

What can I do to make the tax for the subtotal and the total bill be rounded off to two decimal places after the decimal point?

Thanks

Java has a DecimalFormat class for things like this.

http://docs.oracle.com/javase/tutorial/i18n/format/decimalFormat.html

So you would want to add to your code

 DecimalFormat df = new DecimalFormat("###,##0.00");

and change your output to

 double totalwotax = (total * tax );
 System.out.println("The tax for the subtotal is $" + df.format(totalwotax));
 double totalandtax = (total + totalwotax);
 System.out.println("The total for your bill with tax is $" + df.format(totalandtax));

This will ensure there are exactly two digits to the right of the decimal point for your cents, and keep at least one to the left in case the total is under a dollar. If its 1,000 or above, it will be formatted with the comma in the right place. Should your totals be higher than 1 million, you might have to alter it to something like this to get the extra comman

DecimalFormat df = new DecimalFormat("###,###,##0.00");

EDIT: So Java also has built-in support for formatting currency. Forget the DecimalFormatter and use the following:

NumberFormat nf = NumberFormat.getCurrencyInstance();

And then use it just as you would the DecimalFormatter, but without the preceding dollar sign (it will be added by the formatter)

System.out.println("The total for your bill with tax is " + nf.format(totalandtax));

Additionally, this method is locale-sensitive, so if you're in the US it will use dollars, if in Japan it uses yen, and so on.

切勿使用double ,用户BigDecimal

You could simply try this one

DecimalFormat df=new DecimalFormat("#.##");

For Tax

double totalwotax = 0.8130000000000001;
System.out.println("The tax for the subtotal is $" + df.format(totalwotax));

Output: 0.81

For Total

double totalandtax = 14.363000000000001;
System.out.println("The total for your bill with tax is $" + df.format(totalandtax));

Output: 14.36

You can use the format() method instead of println() :

System.out.format("The subtotal is $%.2f%n",  total);  

A description of the format syntax can be found here .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM