简体   繁体   English

将int舍入到小数点后1位?

[英]Rounding int to 1 decimal place?

So in the following set of code I don't understand why "%.1f" will not round y to 1 decimal, I get the following when running the program: 因此,在以下代码集中,我不明白为什么“%.1f”不能将y舍入到小数点后1位,运行程序时得到以下信息:

123 F = Exception in thread "main" 
    java.util.IllegalFormatConversionException: f != java.lang.String
    at java.util.Formatter$FormatSpecifier.failConversion(Formatter.java:4045)
    at java.util.Formatter$FormatSpecifier.printFloat(Formatter.java:2761)
    at java.util.Formatter$FormatSpecifier.print(Formatter.java:2708)
    at java.util.Formatter.format(Formatter.java:2488)
    at java.io.PrintStream.format(PrintStream.java:970)
    at java.io.PrintStream.printf(PrintStream.java:871)
    at Check03A.main(Check03A.java:17)

I also tried Math.round(y * 10) / 10 but it gives me for example 29.0 instead of 28.9 我也尝试过Math.round(y * 10)/ 10,但这给了我例如29.0而不是28.9

import java.util.*;
import java.io.*;
import java.lang.*;
import type.lib.*;

public class Check03A

{
    public static void main(String[] args)
    {
        Scanner scan = new Scanner(System.in);
        PrintStream print = new PrintStream(System.out);
        print.println("Enter the temperature in Fahrenheit");
        int x = scan.nextInt();
        int y = 5 * (x - 32) / 9;
        print.printf(x + " F = " + "%.1f", y + " C");
    }
}

The problem is here: 问题在这里:

print.printf(x + " F = " + "%.1f", y + " C");

There are two arguments to this method: 此方法有两个参数:

  • x + " F = " + "%.1f" (the format string), x + " F = " + "%.1f" (格式字符串),
  • y + "C" (the argument) y + "C" (参数)

Condensing the first argument, the statement becomes: 浓缩第一个参数,该语句变为:

print.printf(x + "F = %.1f", y + "C");

The problem: y + "C" . 问题: y + "C" Why? 为什么? Well, one argument of the + operator is a String ; 好吧, +运算符的一个参数是String therefore, this + becomes a string concatenation operator and what the argument will ultimately be is String.valueOf(y) + "C" . 因此,此+成为字符串连接运算符,参数最终将是String.valueOf(y) + "C"

But the format specification in the format string is %.1f , which expects a float or double , or their boxed equivalents. 但是格式字符串中的格式规范为%.1f ,它需要一个floatdouble或它们的等效形式。 It does not know how to handle a String argument. 它不知道如何处理String参数。

Hence the error. 因此,错误。


There is also the problem that you are doing an integer division, but this is not the topic of this question. 还有一个问题是您要进行整数除法,但这不是此问题的主题。 Provided that all numeric problems are solved, your printing statement will ultimately be: 只要解决了所有数字问题,您的印刷声明最终将是:

print.printf("%.1f F = %.1f C", x, y);
public static void main(String[] args)
    {
        Scanner scan = new Scanner(System.in);
        PrintStream print = new PrintStream(System.out);
        print.println("Enter the temperature in Fahrenheit");
        int x = scan.nextInt();
        //change it
        float y = 5 * (x - 32) / 9;
        print.printf(x + " F = " + "%.1f", y).print(" C");
    }

Make y as float if you want to use %f 如果要使用%f请将y为float

float y = 5 * (x - 32) / 9;
print.printf(x + " F = " + "%.1f", y ).print("C");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM