java四舍五入「java四舍五入的函数」
取小数点前两位,并四舍五入double m=72373589一DecimalFormat df = new DecimalFormatquot#00quotdfformatm二Stringformatquot%2fquot, m。
double x1 = 0026BigDecimal bd = new BigDecimalresult_valueBigDecimal bd2 = bdsetScale1,BigDecimal ROUND_HALF_UPget_double = DoubleParseDoublebd2ToString。
import public class A public static void mainString args String s=quot000000997quot四舍五入,length是小数位数 int length=ssubstringsindexOfquotquot+1lengthString。

Java支持的四舍五入方式参数1ROUND_UP远离零方向舍入向绝对值最大的方向舍入,只要舍弃位非0即进位2ROUND_DOWN趋向零方向舍入向绝对值最小的方向输入,所有的位都要舍弃,不存在进位情况3ROUND_CEILIN。
1首先我们想到用先乘以100取整后再除以1000这种做法Mathround4015*1001000执行结果为401,显然没有达到我们想要的结果2那用呢。
因为double型小数部分会在从二进制变十进制的类型变换中产生误差,所以x2会变成300000003,如果要使输出四舍五入 保留两位小数,可以用String的format函数,方法如下Stringformatquot%2fquot, x1。