设置双格式,带2位小数 [英] set double format with 2 decimal places

查看:163
本文介绍了设置双格式,带2位小数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个简短的等式:

double compute,computed2;

compute=getminutes/60;

其中 getminutes int 我想设置相当于 compute 的2位小数。 0.00 如何格式化等效的2个小数位?

where getminutes is int and I want to set the equivalent of compute with 2 decimal places. 0.00 how can I format the equivalent with 2 decimal places?

示例:

compute=45/60 it should be 0.75 

这是我的工作:

DecimalFormat df2 = new DecimalFormat("00.00000");
double computed,computed2 = 00.000;

computed=60/getitbyminutes;
df2.format(computed);
computed2=computed-8;
df2.format(computed2);

System.out.printf("%1$.2f",computed);
System.out.println();
System.out.printf("%1$.2f",computed2);

输出结果如下:

1.00
7.00 


推荐答案

将其转换为Double

Cast it to Double

compute=(Double)getminutes/60;

然后使用Peter提到的 DecimalFormat

and then use DecimalFormat as mentioned by Peter.

这篇关于设置双格式,带2位小数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆