Why can this floating point text value be parsed as a double?
Asked Answered
R

3

6

Does anybody know why the following snippet does not throw a NumberFormatException?

public class FlIndeed { 

   public static void main(String[] args) { 
      System.out.println(new FlIndeed().parseFloat("0xabcP2f"));
   }

   public float parseFloat(String s) { 

      float f = 0.0f;
      try { 
         f = Float.valueOf(s).floatValue();
         return f;
      }
      catch (NumberFormatException nfe) { 
         System.out.println("Invalid input " + s); 
      }
      finally {
         System.out.println("It's time to get some rest");
         return f; 
      }  
   }
}

Note that there is a P inside .parseFloat("0xabcP2f"));

Regress answered 8/3, 2013 at 11:9 Comment(0)
R
7

Because it do accept hexadecimal values and you are passing a valid hexadecimal value.

From Doc (s = input String argument)

s should constitute a FloatValue as described by the lexical syntax rules:

    FloatValue:
        Signopt NaN 
        Signopt Infinity 
        Signopt FloatingPointLiteral 
        Signopt HexFloatingPointLiteral 
        SignedInteger 

    HexFloatingPointLiteral:
        HexSignificand BinaryExponent FloatTypeSuffixopt 

    HexSignificand:
        HexNumeral 
        HexNumeral . 
        0x HexDigitsopt . HexDigits 
        0X HexDigitsopt . HexDigits 

    BinaryExponent:
        BinaryExponentIndicator SignedInteger 

    BinaryExponentIndicator:
        p 
        P

about NumberFormatException thrown from valueOf

where Sign, FloatingPointLiteral, HexNumeral, HexDigits, SignedInteger and FloatTypeSuffix are as defined in the lexical structure sections of the of the Java Language Specification. If s does not have the form of a FloatValue, then a NumberFormatException is thrown.

About use of p in hex: P in constant declaration

Rockel answered 8/3, 2013 at 11:13 Comment(0)
T
3
 System.out.println(new FlIndeed().parseFloat("0xabcP2f"));

The 0xabcP2f is a HexSignificand value which is legal for Float.valueOf("0xabcP2f"). So it's not throws any exception

For details Please have look on this http://docs.oracle.com/javase/6/docs/api/java/lang/Float.html#valueOf%28java.lang.String%29

Thedrick answered 8/3, 2013 at 11:16 Comment(0)
D
2

http://docs.oracle.com/javase/6/docs/api/java/lang/Float.html

If m is a float value with a normalized representation, substrings are used to represent the significand and exponent fields. The significand is represented by the characters "0x1." followed by a lowercase hexadecimal representation of the rest of the significand as a fraction. Trailing zeros in the hexadecimal representation are removed unless all the digits are zero, in which case a single zero is used. Next, the exponent is represented by "p" followed by a decimal string of the unbiased exponent as if produced by a call to Integer.toString on the exponent value.

So it's used to signify the exponent in the hexadecimal string which you entered, it's a perfectly legal part of the hex string so there's no parsing error and no NumberFormatException thrown.

Decolonize answered 8/3, 2013 at 11:13 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.