[英]Cut down a double number at a certain point after the decimal point
So im having a double and i want my JLabel to display it, i do it like this: 所以我有一个双,我希望我的JLabel显示它,我这样做是这样的:
jlabel.setText(String.valueOf(80.99999D)); // just an example number
But in addition to that, i would like to cut down the double number to maximum 2 digits behind the decimal point. 但除此之外,我想将双精度位数减少到小数点后最多2位。 So in the example above i would like the JLabel to only display 80.99.
因此,在上面的示例中,我希望JLabel仅显示80.99。 Is there any way to do it?
有什么办法吗?
You can solve this by using String.format
您可以使用
String.format
解决此问题
String.format("%.2f", 80.99999);
The number after the .
后面的数字
.
defines, how many digits are displayed after the period (max). 定义在句点(最大)后显示多少位数。 If you write
.02f
you will always have 2 digits after the period. 如果您写
.02f
,则.02f
后将始终有2位数字。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.