首页 > 编程知识 正文

java中除法保留两位小数,java相除保留两位小数

时间:2023-05-06 12:43:42 阅读:179073 作者:2044

如何使用Math.round () :

将两个int类型的数相除后,将保留两位小数。

int a=1188;

int b=93;

double c;

c=(double ) ) math.round ) a*100/b )/100.0 ); //这样可以保持两位数

打印结果: c=0.12

c=newdouble(math.round ) a*1000/b )/1000.0 ); //这样可以保持3位

打印结果: c=0.012

2 .另一种方法

import java.text.DecimalFormat;

decimal format df2=new decimal format (# #.00 ); //这样可以保持两位数

decimal format df2=new decimal format (# #.000 ); //这样可以保持3位

system.out.println(df2.format )双精度类型变量);

PS:

Math.round ()的作用:

double a=123.55

system.out.println (math.round (a ) );

打印结果: 124

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。