用Java打印双号System.out.printf [英] Print double number System.out.printf in Java

查看:162
本文介绍了用Java打印双号System.out.printf的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个双号,我想只打印这个号码的组成部分。我试图使用 System.out.printf 打印它,但我有一个 IllegalFormatConversionException 。我尝试过这样的事情:

I have a double number and i want to print only the integral part of this number. I was trying to print it using System.out.printf but i i got an IllegalFormatConversionException. I tried something like:

A()
{ 
    double x;
    //calculate double
    System.out.println("%d",x);
}

我知道我可以使用系统打印它.out.print 但也会打印小数部分。我怎么能用 printf

I know that i can simply print it using System.out.print but that will print the decimal part too. How can i do this using printf?

推荐答案

System.out.printf("%.0f",x);




  • .0 指定精度。根据此处指定的精度舍入数字。 (例如,如果你想要2个小数位,你会指定0.2)

  • f 指定它是一个浮点 - 包括双精度数(d代表十进制整数)

    • The .0 specifies the precision. Number is rounded off according to the precision specified here. (e.g. if you want 2 decimal places you would specify 0.2)
    • The f specifies it's a floating point - including doubles (d is for decimal integer)
    • 这篇关于用Java打印双号System.out.printf的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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