【Java 数据转换】double型数据由科学计数法转成普通数据(118)

需求:
如:8.796296296296296E-4
转成:0.0008796296296296296
使用:java.math.BigDecimal

代码:


import java.math.BigDecimal;

public class day11 {
	public static void main(String[] args) {
		
		/*
		 * 如何把double型数据由科学计数法转成普通数据;
		 * 如:8.796296296296296E-4
		 * 转成:0.0008796296296296296
		 * 使用:java.math.BigDecimal
		 * 备注:setScale已废弃;也可以使用toString、toPlainString;
		 * 
		 */
		BigDecimal big = new BigDecimal("8.796296296296296E-4");
		BigDecimal a = big.setScale(4,2);
		BigDecimal b = big.setScale(10,2);
		BigDecimal c = big.setScale(2,2);
		String d = String.valueOf(c);
		System.out.println("a:"+a);
		System.out.println("b:"+b);
		System.out.println("c:"+c);
		System.out.println("d:"+d);
		
		String e = big.toString();
		String f = big.toPlainString();

		System.out.println("e:"+e);
		System.out.println("f:"+f);
		
	}
}

测试输出:

a:0.0009
b:0.0008796297
c:0.01
d:0.01
e:0.0008796296296296296
f:0.0008796296296296296

使用场景如:


		String processTime = "";
        if(flag.equals("1")){
            processTime = DateUtil.getCustomTime(endTime_res - startTime_long - penging);
        }
        else if(flag.equals("2")){
            processTime = String.valueOf((endTime_res - startTime_long - penging)/(24.0 * 60.0 * 60.0));
            // double型数据由科学计数法转成普通数据
            if(processTime.contains("E")){
                BigDecimal big = new BigDecimal(processTime);
                BigDecimal c = big.setScale(2,2);
                processTime = String.valueOf(c);
            }
            processTime = processTime.substring(0,processTime.indexOf(".")+2);
        }

你可能感兴趣的:(java,java,开发语言)