Formatting problems using printf in java

2019-09-10 08:56发布

I've read a few other answers on different posts about people having specific problems with line formatting, but have not found anything that has helped enough to solve my problem. I've also looked at the java docs and nothing I have done seems to work.

The last line of code is the one I'm trying to format. I ideally want to format it with 3 decimal precision output.

Double hi = 32.22;
System.out.println("The sqrt. is = " + Math.sqrt(hi));
System.out.printf("The Sqrt Rounded Down Is: " + "/%.3d", Math.floor(Math.sqrt(hi)));

is anything wrong with me "/%.3d", or is it something else? I don't quite fully understand/remember all the formatting options.

1条回答
做个烂人
2楼-- · 2019-09-10 09:45

Try this:

System.out.printf("The Sqrt Rounded Down Is: " + "%.3f", Math.floor(Math.sqrt(hi)));
查看更多
登录 后发表回答