java 小数点后2位小数点,如何将小数点后舍入到小数点后2位(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.

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.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值