This question already has an answer here:
Rounding BigDecimal to *always* have two decimal places
                                
                                    (1个答案)
                                
                        
                                3年前关闭。
            
                    
import java.math.*;

class Moqueet {
    public static void main(String [] args){
        double a, b, c;
        BigDecimal bg1, bg2;
        a = 1;
        b = 10000;
        c = a / b;
        bg1 = new BigDecimal(c);
        MathContext mc = new MathContext(10);
        bg2 = bg1.round(mc);
        System.out.println(bg1);
    }
    }


当我尝试这个我得到如下输出:

0.000100000000000000004792173602385929598312941379845142364501953125


我只希望将我的输出设置为0.0001。我该如何实现?

最佳答案

 bg1 = bg1.setScale(4, RoundingMode.FLOOR);

09-13 12:01