Formatting problems using printf in java

别等时光非礼了梦想. 提交于 2019-12-13 02:55:17

问题


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:


Try this:

System.out.printf("The Sqrt Rounded Down Is: " + "%.3f", Math.floor(Math.sqrt(hi)));


来源:https://stackoverflow.com/questions/25717224/formatting-problems-using-printf-in-java

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!