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

左心房为你撑大大i 提交于 2019-12-01 09:42:56

问题


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


回答1:


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.




回答2:


Never use double for money, user BigDecimal!




回答3:


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




回答4:


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.



来源:https://stackoverflow.com/questions/16700928/how-to-round-decimals-to-2-places-after-the-decimal-point-java

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!