/** * 提供小数位四舍五入处理。 * @param v 需要四舍五入的数字 * @param scale 小数点后保留几位 * @return 四舍五入后的结果 */ public static double round(double v,int scale){
String temp="#,##0.";
for (int i=0;i<scale ;i++ )
{
temp+="0";
}
return Double.valueOf(new java.text.DecimalFormat(temp).format(v));
}
Java带的Math.round不提供精确到位数。上面方法可以完成此功能
String temp="#,##0.";
for (int i=0;i<scale ;i++ )
{
temp+="0";
}
return Double.valueOf(new java.text.DecimalFormat(temp).format(v));
}
Java带的Math.round不提供精确到位数。上面方法可以完成此功能
解决方案 »
- 在Ubuntu下安装jdk 出现./install.sfx.2354: not found无法安装成功
- 问个SQL的问题
- Exec zidian..xp_cmdshell 导入时如果需要导入两个字段怎么办?
- 什么情况帮忙分析一下:An error has occurred.see error log for more details.
- struts2.0配置的问题
- jaspersoft ireport4.6.0 制作折线图问题
- 有没有比较好的jdk1.5源代码分析文章?
- 数据库查询排序应该在数据库中实现,还是在服务器端实现?
- 用set path = %path%;会覆盖原来的用户变量?
- jbuilderx+weblogic81的问题,高手来看看啊,散分!
- [struts]语言资源文件里的一个键的值如果是多行怎办?
- 怎样才能包含j2ee的类库,如javax.ejb? 我的环境是netbeans4.1+Sun AS8+PointDatabase
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 dC1 = (double)((int)(dC * 100) + 0.5))/100.0;
dC=(double)Math.round(dC*100)/100;
System.out.print(dC);