-2
    float a = (float) 3.32 ; 
    float b = (float) 4.32 ; 
    System.out.println(b%a);

output on my machine is 1.0000002

Why ?

RamRasia
  • 51
  • 1
  • 7

1 Answers1

2

First how to declare a float value as a variable?

float val=3.23f;

About your result. It is a typical nature of float .

How to get correct answer? try this way

 BigDecimal a = new BigDecimal("3.32") ;
 BigDecimal b = new BigDecimal("4.32") ;
 System.out.println(b.divideAndRemainder(a)[1]); // 1st element is remainder 

Out put:

 1

You may need to read about divideAndRemainder()

Ruchira Gayan Ranaweera
  • 34,993
  • 17
  • 75
  • 115