修剪双精度到2位小数 [英] Trim Double to 2 decimal places

查看:67
本文介绍了修剪双精度到2位小数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

应该很容易。我最初会在javascript中执行此操作,但必须在设置到我的处理程序页面中的表单之前执行此操作。无论如何,我需要使这些值有2位小数。 Ex 219333.5888888需要219333.58。是否有修剪函数?

should be an easy one. I originally was gonna do this in javascript but have to do it prior to setting to the form in my handler page. Anyway I need to make these values have 2 decimal places. Ex 219333.5888888 needs to be 219333.58. Is there a trim function or something?

 form.setUnitRepairCost(Double.toString(jobPlanBean.getUnitTotalCost()));   //UNIT REPAIR COST
 form.setUnitMaterialCost(Double.toString(jobPlanBean.getUnitTotalMaterialCost())); //UNIT MATERIAL COST


推荐答案

这是一个简单的例子格式化十进制值

here is the simple example to format the decimal value

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));
    }

}

这篇关于修剪双精度到2位小数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆