How to format a variable of double type

冷暖自知 提交于 2019-12-11 02:39:13

问题


I have a variable of double type representing a value of about 34.323423423123456. I'd like to format it to have only two decimal places (e.g. 34.32), rounded properly; e.g. 2.229934345 would become 2.23.

I am new to Java and none the tricks I know from C# worked.


回答1:


Related to this Question use DecimalFormat setRoundingMode




回答2:


Here's an example format string, I believe it correctly rounds the resulting value. See Markus' post if you need more control over the rounding or format:

String.format("%.2f", 2.229934345);


来源:https://stackoverflow.com/questions/649447/how-to-format-a-variable-of-double-type

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