-3

When I tried this Code:

double d=1.23;
        System.out.println(d%1);

The output was: 0.22999999999999998

My Question is: Why?

Anonymous
  • 171
  • 1
  • 2
  • 9
MuMtaZ
  • 78
  • 1
  • 7

1 Answers1

1

The short answer is that double is not accurate. The long answer is to understand that you should learn the way double stored in memory

if you want to do accurate calculations, i would use BigDecimal object of java

    BigDecimal num = new BigDecimal("1.23");
    System.out.println(num.remainder(new BigDecimal(1)));
Dima
  • 8,586
  • 4
  • 28
  • 57
  • 1
    While this answer may theoretically answer the question, [it is better](http://meta.stackexchange.com/q/8259) to include the essential parts of the answer here, and provide the link for reference. As of now, this answer doesn't tell the OP what they're doing wrong, how to fix it, and so on. – hichris123 Feb 15 '14 at 17:30
  • edited, added suggestion – Dima Feb 15 '14 at 17:32