如何在Java中将输出的十进制数格式化为小数点后两位?

huangapple go评论75阅读模式
英文:

How do I format the output decimal to two places after the decimal in java?

问题

我希望我的 fallingTime 数字限制在两位小数。我使用了 "String.format(%.2f)" 但是出现了错误信息。

这是错误信息:

Exception in thread "main" java.util.IllegalFormatConversionException: f != java.lang.String
        at java.base/java.util.Formatter$FormatSpecifier.failConversion(Formatter.java:4426)
        at java.base/java.util.Formatter$FormatSpecifier.printFloat(Formatter.java:2951)
        at java.base/java.util.Formatter$FormatSpecifier.print(Formatter.java:2898)
        at java.base/java.util.Formatter.format(Formatter.java:2673)
        at java.base/java.util.Formatter.format(Formatter.java:2609)
        at java.base/java.lang.String.format(String.java:3302)
        at fallingDistance.main(fallingDistance.java:22)
public class fallingDistance {

    public static double fallDistance(int fallingTime){

        double distance;
        double g = 9.8;
        distance = (0.5*g*Math.pow(fallingTime, 2)); // 修改这一行
        return distance;

    }

    //public static void displayHeader(){
        //System.out.println("Time\t\t\t\t\tDistance\n----\t\t\t\t\t--------");
    //}
    public static void main(String[] args){

        //displayHeader();

        for(int time = 1; time <= 10; time++){
            System.out.println(time + " seconds\t\t" + String.format("%.2f", fallDistance(time)) + " meters"); // 修改这一行

        }
    }
}
英文:

I want my fallingTime numbers to be limited to two decimal places. I used "String.format(%.2f)" but it is giving me an error message.

This is the error message:

Exception in thread &quot;main&quot; java.util.IllegalFormatConversionException: f != java.lang.String
        at java.base/java.util.Formatter$FormatSpecifier.failConversion(Formatter.java:4426)
        at java.base/java.util.Formatter$FormatSpecifier.printFloat(Formatter.java:2951)
        at java.base/java.util.Formatter$FormatSpecifier.print(Formatter.java:2898)
        at java.base/java.util.Formatter.format(Formatter.java:2673)
        at java.base/java.util.Formatter.format(Formatter.java:2609)
        at java.base/java.lang.String.format(String.java:3302)
        at fallingDistance.main(fallingDistance.java:22)
public class fallingDistance {


    public static double fallDistance(int fallingTime){

        double distance;
        double g = 9.8;
        distance = (0.5*g*Math.pow(fallingTime, fallingTime));
        return distance;
    

    }

    //public static void displayHeader(){
        //System.out.println(&quot;Time\t\t\t\t\tDistance\n----\t\t\t\t\t--------&quot;);
    //}
    public static void main(String[] args){
        
        //displayHeader();
        
        for(int time = 1; time &lt;= 10; time++){
            System.out.println(time+ &quot; seconds\t\t&quot; + String.format(&quot;%.2f&quot;, fallDistance(time) + &quot; meters&quot;));

        }
    }
}

答案1

得分: 1

替换 +,

System.out.println(time + " seconds\t\t" + String.format("%.2f", fallDistance(time)) + " meters");
英文:

Replace + with ,

System.out.println(time + &quot; seconds\t\t&quot; + String.format(&quot;%.2f&quot;, fallDistance(time), &quot; meters&quot;));

答案2

得分: 1

你误放了一个括号。以下应该可以工作。

for (int time = 1; time <= 10; time++) {
	System.out.println(time + "秒\t\t" + String.format("%.2f", fallDistance(time)) + "米");
}
英文:

You had misplaced a bracket. The below should work.

for (int time = 1; time &lt;= 10; time++) {
	System.out.println(time + &quot; seconds\t\t&quot; + String.format(&quot;%.2f&quot;, fallDistance(time)) + &quot; meters&quot;);

}

答案3

得分: 1

我从代码末尾移除了“ meters”,然后放在“%.2f”之后,这样就可以了。代码现在看起来是这样的:

{System.out.println(time + "秒\t\t" + String.format("%.2f 米", fallDistance(time)));}

英文:

I removed " meters" from the end of the code and put it after "%.2f" and that worked. The code looks like this now:

{System.out.println(time + " seconds\t\t" + String.format("%.2f meters", fallDistance(time)));}

huangapple
  • 本文由 发表于 2020年10月10日 23:49:34
  • 转载请务必保留本文链接:https://go.coder-hub.com/64295311.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定