printf方法中的多个参数 [英] multiple arguments in a printf method

查看:135
本文介绍了printf方法中的多个参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为Java类编写代码,要求我创建类似于帐单/收据的代码.类似于:

I am writing a code for my Java class that calls for me to create something that looks like a bill/receipt. similar to:

    Item                    quantity                    price            total
    gum                     10                          $1.49            $14.90
    soda                    3                           $1.00            $3.00
    chips                   20                          $1.25            $20.00

我正在使用printf();方法.我声明的变量是

I am using the printf(); method. The variables i have declared are

    double gumPrice = 1.49;
    float gumQuantity = 10;
    double gumTotal = 14.90;
    double sodaPrice = 1.00;
    float sodaQuantity = 3;
    double sodaTotal = 3.00;
    double chipsPrice = 1.25;
    float chipsQuantity = 20;
    double chipsTotal = 25.00;

我需要创建一些printf();语句以这种方式列出数据.

i need to create a few printf(); statements to list the data in that manner.

这是我到目前为止所需要的口香糖,这给了我一个错误.

This is what i have so far for gum, which is giving me an error.

        System.out.printf("%-30s %-10.2f %-10.2f %-5.2f", "Gum",gumQuantity,"$"+gumPrice,"$"+gumTotal);

将double放入printf()的正确方法是什么?同时在一行中列出多个参数?

What is the correct way to put a double in a printf(); while listing multiple arguments in one line?

推荐答案

最初的问题是由于两种格式Java输出格式的错误混合使用: String 串联和 printf 格式化.如注释中所指出的,"$" + gumPrice 是一个字符串,而不是双精度型.它必须具有%s 格式,而不是 printf 中的%f .但是,最好选择一种格式化方法并坚持使用.

The original problem was due to incorrect mixing of two methods of formatting Java output, String concatenation and printf formatting. As pointed out in comments, "$"+gumPrice is a String, not a double. It would have to have a %s format, not %f in printf. However, it would be better to pick one way of doing the formatting, and stick with it.

使用 printf 方法(在这种情况下我认为更好),输出行变为:

Going with the printf approach, which I think it better in this situation, the output line becomes:

System.out.printf(%-30s%-10.2f $%-10.2f $%-5.2f","Gum",gumQuantity,gumPrice,gumTotal);

它会打印:

Gum                            10.00      $1.49       $14.90

如果使用String串联方法,则需要使用 DecimalFormat 显式转换 doubles 来强制使用小数点后两位.

If you went with the String concatenation approach, you would need to explicitly convert the doubles using a DecimalFormat to force the two decimal places.

避免对金额使用 double 的原因是,许多短终止小数部分只能近似表示.您的 gumPrice 实际上是1.48999999999999999999911182158029987476766109466552734375,与您想要的非常不同.另一方面, new BigDecimal("1.49")恰好是1.49.在某些情况下,尤其是如果您需要截断而不是四舍五入,由 double 引入的微小差异可能很重要,因此 BigDecimal 对于货币计算而言更安全.

The reason to avoid double for money amounts is that many short terminating decimal fractions can only be represented approximately. Your gumPrice is actually 1.4899999999999999911182158029987476766109466552734375, which is very, very slightly different from what you wanted. On the other hand, new BigDecimal("1.49") is exactly 1.49. In some situations, especially if you need to truncate rather than round to nearest, the tiny differences introduced by double can matter, so BigDecimal is safer for currency calculations.

这篇关于printf方法中的多个参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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