2013-06-13 123 views
2

设置双格式的我有一个简短的公式:有2位小数

double compute,computed2; 

compute=getminutes/60; 

其中getminutesint,我想设置的compute有2位小数的等价物。 0.00如何格式化带有2位小数的等价物?

例子:

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 
+0

“* compute = 45/60它应该是0.75 *”不在Java中。尝试45.0d/60.0d –

回答

4

铸造它为Double

compute=(Double)getminutes/60; 

,然后用DecimalFormat由彼得提及。

+0

Yeap!谢谢你解决了我的问题。为什么我不能接受你的答案? – Maguzu

+0

@JonSnow:很高兴能帮到你!你收到什么错误信息? – xyz

8

只是格式化输出的正确方法:

double d = 1.234567; 
DecimalFormat df = new DecimalFormat("#.##"); 
System.out.print(df.format(d)); 
+1

计算= 45/60它应该是0.75。我的double没有声明...这是一个整数。 – Maguzu