1 Reply Latest reply: Aug 2, 2012 4:43 AM by EJP RSS

    Precision lost in float value though it is lesser than Float.MAX_VALUE

    Raguraman
      Hi,

      Given below the test code and its output. When I get float value from number value, precision is lost.. Can anyone tell me why this behavior also how to handle this? Thanks
              public static void main(String[] args)
          {
              try
              {
                  java.lang.Number numberVal = 676543.21;
                  float floatVal = numberVal.floatValue();
                  System.out.println("Number value    : " + numberVal);
                  System.out.println("float value     : " + floatVal);
                  System.out.println("Float.MAX_VALUE : " + Float.MAX_VALUE);
                  System.out.println("Is floatVal > Float.MAX_VALUE ? " + ( floatVal > Float.MAX_VALUE));
              }catch(Exception e)
              {
                  e.printStackTrace();
              }
          }
      Output:
      Number value    : 676543.21
      float value     : 676543.2
      Float.MAX_VALUE : 3.4028235E38
      Is floatVal > Float.MAX_VALUE ? false