double型怎样保留两位小数

frogandfish 2005-06-28 06:35:59
从数据库中取的double型,在界面上要显示为保留两位小数形式,怎样写?
...全文
1627 1 打赏 收藏 转发到动态 举报
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
mofeir 2005-06-28
  • 打赏
  • 举报
回复
要String得话,可以用NumberFormat
public class Test {

public static void main(String[] args) {
float[] float_args = {12345.26f, 1234.22f, 123.22f, 0.123456789f};
for (int i = 0; i < float_args.length; i++) {
System.out.println(floatFormat(float_args[i]));
}

double[] double_args = {12345.26d, 1234.22d, 123.22d, 0.123456789d};
for (int i = 0; i < double_args.length; i++) {
System.out.println(doubleFormat(double_args[i]));
}

}

public static String floatFormat(float f) {
DecimalFormat df = new DecimalFormat("0.#");
return df.format(f);
}

public static String doubleFormat(double d) {
DecimalFormat df = new DecimalFormat("0.##");
return df.format(d);
}
}

还是要double的话
如下
double dC = 1000.2356;
dC=(double)Math.round(dC*100)/100;
System.out.print(dC);

81,092

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧