0

Im having problems with float data type in getting 1 or 2 decimal places only..

but if you look at the code seems okay to me but the output is different.. I dont want to convert into string yet co'z I need to do something else before the printing them out..

Just for testing purposes I did this literally. There are variables and workarounds just to simplify everything. It suppose to end at 12.8 but it yields 12.8000001 that's why it skips my testing..

float oh=11.0f;
    int retval=1;
      while (oh<=12.8)
        {
        //System.out.printf("%-11s",dec_format.format(oh)+'K');
     System.out.println(oh);
        oh=oh + 0.3f;
     }

output is: 11.0 11.3 11.6 11.900001 12.200001 12.500001

It should end at 12.8 and why with ....00001??

radj1973
  • 1
  • 1

1 Answers1

0

System.out.printf("%2.3f", 2.887);

prints 2.887

System.out.printf("%2.2f", 2.887);

prints 2.89

System.out.printf("%2.1f", 2.887);

prints 2.9

Olavi Mustanoja
  • 2,045
  • 2
  • 23
  • 34