Java:打印双精度 - println 或 printf

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/34063620/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 15:16:42  来源:igfitidea点击:

Java: print double - println or printf

javaprintfprintln

提问by northerner

Suppose I have the following code:

假设我有以下代码:

double median = med(10.0, 12.0, 3.0); //method returns middle number as double

Now, I want to write a message stating the median. Can this be done using println(), like the following:

现在,我想写一条消息说明中位数。这可以使用println()来完成,如下所示:

System.out.println("Your number was " + median);

Or must I use printf()with double values?

或者我必须使用带有双精度值的printf()吗?

The reason I ask is because I have seen both practices online 1, but when I try using println()I get an error, unless i write:

我问的原因是因为我在网上看到了两种做法1,但是当我尝试使用println() 时,我得到一个错误,除非我写:

System.out.println(median);

Hence, can you use println()to print out doublevalues and strings, or must you use printf()for that purpose?

因此,您可以使用println()来打印精度值和字符串,还是必须为此使用printf()

采纳答案by Abdelhak

The printfmethod can be particularly useful when displaying multiple variables in one line which would be tedious using string concatenation:
The println()which can be confusing at times. (Although both can be used in almost all cases).

printf:在一行中这将使用字符串连接是乏味显示多个变量时,方法可以是特别有用
println(),其可以在某些时候,混淆。(虽然两者都可以在几乎所有情况下使用)。

 double a = 10;
 double b = 20;

System.out.println("a: " + a + " b: " + b);// Tedious string concatenation.

System.out.printf("a: %f b: %f\n", a, b);// Output using string formatting.

Output:

输出:

a: 10.0 b: 20.0
a: 10,000000 b: 20,000000

回答by Michael Lloyd Lee mlk

You can do both(if System.out.println("Your number was " + median);is not working you have something else wrong. Show us the stack trace). printfallows you some more formatting options(such as the number of decimal places) without having to use a DecimalFormat, but it is a choice on style rather than anything else.

你可以两者都做(如果System.out.println("Your number was " + median);不工作,你有其他错误。向我们展示堆栈跟踪)。printf允许您使用更多格式选项(例如小数位数)而不必使用DecimalFormat,但它是一种风格选择,而不是其他任何东西。

Personally I find string concatenation easer on the eyes and easer to maintain. The %blahgets lost in the string, errors in string concatenation is a compile time error and adding a new variable requires less thought. However I'm in the minority on this.

就我个人而言,我发现字符串连接更容易,而且更容易维护。该%blah得到的字符串中迷失,在字符串连接错误是一个编译时错误并增加了新的变数需要较少的思想。不过我在这方面是少数。

The pros in printfis that you don't need to construct a bunch of formatters, others find its format easer on the eyes and when using a printf like method provided by a logging framework the string building only happens if actually required.

优点printf是你不需要构建一堆格式化程序,其他人发现它的格式更容易,当使用日志框架提供的类似 printf 的方法时,字符串构建只在实际需要时发生。

double value = 0.5;
System.out.println("value: " + value);
System.out.printf("value: %f", value);

If you want to set a number of decimal places:

如果要设置小数位数

    double value = 0.5;
    // String concatenation 
    DecimalFormat df = new DecimalFormat("0.000");
    System.out.println("value: " + df.format(value));

    // printf
    System.out.printf("value: %.3f", value);