Java 设置双精度格式,小数点后两位
我有一个简短的等式:Java 设置双精度格式,小数点后两位,java,swing,int,decimal,decimalformat,Java,Swing,Int,Decimal,Decimalformat,我有一个简短的等式: double compute,computed2; compute=getminutes/60; 其中getminutes是int,我想将compute的等价项设置为小数点后两位0.00如何将等效值设置为小数点后两位 例如: compute=45/60 it should be 0.75 以下是我的作品: DecimalFormat df2 = new DecimalFormat("00.00000"); double computed,computed2 = 00
double compute,computed2;
compute=getminutes/60;
其中getminutes
是int
,我想将compute
的等价项设置为小数点后两位<代码>0.00如何将等效值设置为小数点后两位
例如:
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 d = 1.234567;
DecimalFormat df = new DecimalFormat("#.##");
System.out.print(df.format(d));
投双倍
compute=(Double)getminutes/60;
然后使用彼得提到的
DecimalFormat
。compute=45/60它应该是0.75。我的替身没有申报。。。这是一个整数。在Java中,“compute=45/60应该是0.75”。试试45.0d/60.0d!谢谢,这解决了我的问题。为什么我不能接受你的回答?@琼斯诺:很高兴我能帮上忙!您收到了什么错误消息?