-1

I'm trying to take the number after the dot (.)

for ex: 5.12 what I want is 0.12

this is my code:-

    double number= 5.12;
    int nInt= (int) number;
    double nDouble= number- nInt;

    if (nDouble== 0.12) {

        System.out.println(nDouble);

    } else {

        System.err.println(nDouble+ "\nThe answer should be (0.12)!!!");
    }

the code should give me 0.12 but it gives me 0.1200000000000001

How to fix this?

jonrsharpe
  • 115,751
  • 26
  • 228
  • 437
Mohammad Khair
  • 436
  • 6
  • 15

1 Answers1

-2

float nf = (float) nDouble;

try this!