Format of BigDecimal number

前端 未结 4 1976
太阳男子
太阳男子 2020-12-19 20:08
BigDecimal val = BigDecimal.valueOf(0.20);
System.out.println(a);

I want to store in val a value 0.20 and not 0.2. What I

4条回答
  •  猫巷女王i
    2020-12-19 20:39

    You could use the String constructor of BigDecimal. It preserves the scale (which is what you want).

    BigDecimal val = new BigDecimal("0.20");
    

    See http://docs.oracle.com/javase/1.5.0/docs/api/java/math/BigDecimal.html#BigDecimal(java.lang.String)

提交回复
热议问题