方法 /** * 使用java正则表达式去掉多余的.与0 * @param s * @return */ public static String subZeroAndDot(String s){ if(s.indexOf(".") > 0){ s = s.replaceAll("0+?$", "");//去掉多余的0 s = s.replaceAll("[.]$", "");//如最后一位是.则去掉…
Java中double类型的数据精确到小数点后两位 多余位四舍五入,四种方法 一: double f = 111231.5585;BigDecimal b = new BigDecimal(f); double f1 = b.setScale(2,BigDecimal.ROUND_HALF_UP).doubleValue(); 二: new java.text.DecimalFormat("#.00").format(3.1415926) 三: double d = 3.1415926…
一.问题的提出: 如果我们编译运行下面这个程序会看到什么?public class Test{    public static void main(String args[]){        System.out.println(0.05+0.01);        System.out.println(1.0-0.42);        System.out.println(4.015*100);        System.out.println(123.3/100);    }};你没…
一.需求:从数据表中读出一个double的数据,比如是3.5,没问题,但是如果再用3.5进行计算,比如乘以100,结果就是350了,而是35000000004 因为是浮点运算,所有语言中的浮点数都会有这个问题,所以浮点数只能用作科学运算 可以使用 java.math.BigDecimal 类解决,这里简单小计一下: BigDecimal a1 = new BigDecimal(Double.toString(mydouble); BigDecimal b1 = new BigDecimal(Do…
该工具类所在的包:import java.math.BigDecimal; 项目中使用该工具类的相关代码: // 如果Output表中已经存在该节点(插入数据的节点)的信息,则修改Output表中的这条信息,其中完工数量就是获取的数据的完工数量加上已经存在的节点的完工数量,另外还要修改Input表中的数据 output.setId(outputList1.get(0).getId()); Double completeDty1 = Double.valueOf(StringUtils.isEmp…
4种方法,都是四舍五入,例: import java.math.BigDecimal; import java.text.DecimalFormat; import java.text.NumberFormat; public class format { double f = 111231.5585; public void m1() { BigDecimal bg = new BigDecimal(f); double f1 = bg.setScale(2, BigDecimal.ROUND…
java代码: public void IntA(int a , int b){ //首先判断分母不能为0 if(b!=0){ folat num = (float) a*100/b; DecimalFormat df = new DecimalFormat("0.00");//格式化小数 , 其实这里还可以这样写 DecimalFormat("0.00%"); 这样就不用在最后输出时还要加. String s = df.format(num); } } 1.fol…
DecimalFormat percent = new DecimalFormat("0.00%"); completed_num = (double) involvedTask_finished/userInvolvedTask_sum;completed_rate = percent.format(completed_num); 来自为知笔记(Wiz)…
https://blog.csdn.net/wcxiaoych/article/details/42806313…
package com.flf.util;import java.math.BigDecimal;/** * double类型数据运算 * @author Yancy 2016-12-14 * */public class ArithUtils{ //默认除法运算精度//private static final int DEF_DIV_SCALE = 10; //这个类不能实例化 private ArithUtils(){ } /** * 提供精确的加法运算. * @param v1 被加数 *…