如何将小数点后舍入到小数点后2位(Java) [英] How to Round Decimals to 2 Places after the Decimal Point (Java)

查看:209
本文介绍了如何将小数点后舍入到小数点后2位(Java)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对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?

编写一个可以模拟收银机的程序。提示用户输入三个项目的价格。将它们加在一起得出小计。确定小计的税(6%)。找到销售总额小计加税。显示每个项目的价格,小计金额,税额和最终金额。

到目前为止,我已经知道了:

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);

    }
}

输出(如果价格为假设price1 = 1.65,price2 = 2.82,price3 = $ 9.08)如下:

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


请为第一个商品$ 1.65的价格出价

Please anter a price for item number one $1.65

请输入第二个商品的价格$ 2.82

Please enter a price for item number two $2.82

请输入第三个商品的价格$ 9.08

Please enter a price for item number three $9.08

小计为$ 13.55

The subtotal is $13.55

小计的税额为$ 0.8130000000000001

The tax for the subtotal is $0.8130000000000001

您的含税账单总额为$ 14.363000000000001

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?

谢谢

推荐答案

Java具有类似这样的DecimalFormat类。

Java has a DecimalFormat class for things like this.

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

所以您想添加到代码中

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

,然后将输出更改为

 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));

这将确保美分的小数点右边有两位数字,并保持如果总数少于一美元,则至少向左移一个。如果其等于或大于1,000,则会在正确的位置使用逗号格式化。如果您的总数超过一百万,则可能必须将其更改为类似的格式以获取多余的逗号

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");

编辑:
因此Java也具有内置支持用于格式化货币。忘记DecimalFormatter并使用以下命令:

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

NumberFormat nf = NumberFormat.getCurrencyInstance();

然后像使用DecimalFormatter一样使用它,但是没有前面的美元符号(它将是

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位(Java)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆