BigDecimal to string

后端 未结 8 1760
感情败类
感情败类 2020-12-13 08:06

I have a BigDecimal object and i want to convert it to string. The problem is that my value got fraction and i get a huge number (in length) and i only need the original num

8条回答
  •  眼角桃花
    2020-12-13 08:44

    If you just need to set precision quantity and round the value, the right way to do this is use it's own object for this.

    BigDecimal value = new BigDecimal("10.0001");
    value = value.setScale(4, RoundingMode.HALF_UP);
    System.out.println(value); //the return should be "10.0001"
    

    One of the pillars of Oriented Object Programming (OOP) is "encapsulation", this pillar also says that an object should deal with it's own operations, like in this way:

提交回复
热议问题