1
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);
    }
    }

when I try this I get output as follow:

0.000100000000000000004792173602385929598312941379845142364501953125

I want my out put as 0.0001 only. How can I achive this?

Reporter
  • 3,897
  • 5
  • 33
  • 47
Abdul.Moqueet
  • 902
  • 12
  • 19
  • 3
    http://stackoverflow.com/questions/15643280/rounding-bigdecimal-to-always-have-two-decimal-places – Ricardo Oct 18 '16 at 10:05
  • Note that "scaling" and "rounding" are two different things. You just want rounding here. Also note that you're printing `bg1` and ignoring `bg2`. – Jon Skeet Oct 18 '16 at 10:06

1 Answers1

0
 bg1 = bg1.setScale(4, RoundingMode.FLOOR);
Jay Prakash
  • 787
  • 6
  • 22