我想在运行时将一个数字转换为2位小数(总是显示两位小数)。我尝试了一些代码,但它只起作用,如下所示
20.03034 >> 20.03
20.3 >> 20.3 ( my code only rounds not converts )
但是,我希望它这样做:
20.03034 >> 20.03
20.3 >> 20.30 (convert it to two decimal places)
我的代码如下:
angle = a variable
angle_screen = a variable
DecimalFormat df = new DecimalFormat("#.##");
angle = Double.valueOf(df.format(angle));
angle_screen.setText(String.valueOf(angle) + tmp);
如果能帮上忙,那就太好了,谢谢。
发布于 2012-01-09 03:26:05
试试这个:String.format("%.2f", angle);
发布于 2012-01-09 01:38:37
试一试
DecimalFormat df = new DecimalFormat("#,##0.00");
https://stackoverflow.com/questions/8779659
复制相似问题