修剪双精度位到小数点后两位
问题内容:
应该很容易。我本来是用javascript来做的,但是在设置我的处理程序页面中的表单之前必须先做。无论如何,我需要使这些值具有2个小数位。Ex
219333.5888888必须为219333.58。有修剪功能或其他功能吗?
form.setUnitRepairCost(Double.toString(jobPlanBean.getUnitTotalCost())); //UNIT REPAIR COST
form.setUnitMaterialCost(Double.toString(jobPlanBean.getUnitTotalMaterialCost())); //UNIT MATERIAL COST
问题答案:
这是格式化十进制值的简单示例
import java.text.*;
public class DecimalPlaces {
public static void main(String[] args) {
double d = 1.234567;
DecimalFormat df = new DecimalFormat("#.##");
System.out.print(df.format(d));
}
}