java.lang.Object java.lang.Number java.lang.Float
All Implemented Interfaces:
Comparable, Serializable
In addition, this class provides several methods for converting a {@code float} to a {@code String} and a {@code String} to a {@code float}, as well as other constants and methods useful when dealing with a {@code float}.
Lee
 BoyntonArthur
 van HoffJoseph
 D. DarcyJDK1.0
 Field Summary  

public static final float  POSITIVE_INFINITY  A constant holding the positive infinity of type {@code float}. It is equal to the value returned by {@code Float.intBitsToFloat(0x7f800000)}. 
public static final float  NEGATIVE_INFINITY  A constant holding the negative infinity of type {@code float}. It is equal to the value returned by {@code Float.intBitsToFloat(0xff800000)}. 
public static final float  NaN  A constant holding a NotaNumber (NaN) value of type {@code float}. It is equivalent to the value returned by {@code Float.intBitsToFloat(0x7fc00000)}. 
public static final float  MAX_VALUE  A constant holding the largest positive finite value of type {@code float}, (22^{23})·2^{127}. It is equal to the hexadecimal floatingpoint literal {@code 0x1.fffffeP+127f} and also equal to {@code Float.intBitsToFloat(0x7f7fffff)}. 
public static final float  MIN_NORMAL  A constant holding the smallest positive normal value of type
{@code float}, 2^{126}. It is equal to the
hexadecimal floatingpoint literal {@code 0x1.0p126f} and also
equal to {@code Float.intBitsToFloat(0x00800000)}.

public static final float  MIN_VALUE  A constant holding the smallest positive nonzero value of type {@code float}, 2^{149}. It is equal to the hexadecimal floatingpoint literal {@code 0x0.000002P126f} and also equal to {@code Float.intBitsToFloat(0x1)}. 
public static final int  MAX_EXPONENT  Maximum exponent a finite {@code float} variable may have. It
is equal to the value returned by {@code
Math.getExponent(Float.MAX_VALUE)}.

public static final int  MIN_EXPONENT  Minimum exponent a normalized {@code float} variable may have.
It is equal to the value returned by {@code
Math.getExponent(Float.MIN_NORMAL)}.

public static final int  SIZE  The number of bits used to represent a {@code float} value.

public static final Class<Float>  TYPE  The {@code Class} instance representing the primitive type
{@code float}.

Constructor: 

public Float(float value){ this.value = value; } 
public Float(double value){ this.value = (float)value; } 
public Float(String s) throws NumberFormatException{ // REMIND: this is inefficient this(valueOf(s).floatValue()); }

Method from java.lang.Float Summary: 

byteValue, compare, compareTo, doubleValue, equals, floatToIntBits, floatToRawIntBits, floatValue, hashCode, intBitsToFloat, intValue, isInfinite, isInfinite, isNaN, isNaN, longValue, parseFloat, shortValue, toHexString, toString, toString, valueOf, valueOf 
Methods from java.lang.Number: 

byteValue, doubleValue, floatValue, intValue, longValue, shortValue 
Methods from java.lang.Object: 

clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait 
Method from java.lang.Float Detail:  

public byte byteValue(){ return (byte)value; }
 
public static int compare(float f1, float f2){ if (f1 < f2) return 1; // Neither val is NaN, thisVal is smaller if (f1 > f2) return 1; // Neither val is NaN, thisVal is larger // Cannot use floatToRawIntBits because of possibility of NaNs. int thisBits = Float.floatToIntBits(f1); int anotherBits = Float.floatToIntBits(f2); return (thisBits == anotherBits ? 0 : // Values are equal (thisBits < anotherBits ? 1 : // (0.0, 0.0) or (!NaN, NaN) 1)); // (0.0, 0.0) or (NaN, !NaN) }
new Float(f1).compareTo(new Float(f2))  
public int compareTo(Float anotherFloat){ return Float.compare(value, anotherFloat.value); }
 
public double doubleValue(){ return (double)value; }
 
public boolean equals(Object obj){ return (obj instanceof Float) && (floatToIntBits(((Float)obj).value) == floatToIntBits(value)); }
Note that in most cases, for two instances of class {@code Float}, {@code f1} and {@code f2}, the value of {@code f1.equals(f2)} is {@code true} if and only if f1.floatValue() == f2.floatValue() also has the value {@code true}. However, there are two exceptions:  
public static int floatToIntBits(float value){ int result = floatToRawIntBits(value); // Check for NaN based on values of bit fields, maximum // exponent and nonzero significand. if ( ((result & FloatConsts.EXP_BIT_MASK) == FloatConsts.EXP_BIT_MASK) && (result & FloatConsts.SIGNIF_BIT_MASK) != 0) result = 0x7fc00000; return result; }
Bit 31 (the bit that is selected by the mask {@code 0x80000000}) represents the sign of the floatingpoint number. Bits 3023 (the bits that are selected by the mask {@code 0x7f800000}) represent the exponent. Bits 220 (the bits that are selected by the mask {@code 0x007fffff}) represent the significand (sometimes called the mantissa) of the floatingpoint number. If the argument is positive infinity, the result is {@code 0x7f800000}. If the argument is negative infinity, the result is {@code 0xff800000}. If the argument is NaN, the result is {@code 0x7fc00000}. In all cases, the result is an integer that, when given to the #intBitsToFloat(int) method, will produce a floatingpoint value the same as the argument to {@code floatToIntBits} (except all NaN values are collapsed to a single "canonical" NaN value).  
public static native int floatToRawIntBits(float value)
Bit 31 (the bit that is selected by the mask {@code 0x80000000}) represents the sign of the floatingpoint number. Bits 3023 (the bits that are selected by the mask {@code 0x7f800000}) represent the exponent. Bits 220 (the bits that are selected by the mask {@code 0x007fffff}) represent the significand (sometimes called the mantissa) of the floatingpoint number. If the argument is positive infinity, the result is {@code 0x7f800000}. If the argument is negative infinity, the result is {@code 0xff800000}. If the argument is NaN, the result is the integer representing the actual NaN value. Unlike the {@code floatToIntBits} method, {@code floatToRawIntBits} does not collapse all the bit patterns encoding a NaN to a single "canonical" NaN value. In all cases, the result is an integer that, when given to the #intBitsToFloat(int) method, will produce a floatingpoint value the same as the argument to {@code floatToRawIntBits}.  
public float floatValue(){ return value; }
 
public int hashCode(){ return floatToIntBits(value); }
 
public static native float intBitsToFloat(int bits)
If the argument is {@code 0x7f800000}, the result is positive infinity. If the argument is {@code 0xff800000}, the result is negative infinity. If the argument is any value in the range {@code 0x7f800001} through {@code 0x7fffffff} or in the range {@code 0xff800001} through {@code 0xffffffff}, the result is a NaN. No IEEE 754 floatingpoint operation provided by Java can distinguish between two NaN values of the same type with different bit patterns. Distinct values of NaN are only distinguishable by use of the {@code Float.floatToRawIntBits} method. In all other cases, let s, e, and m be three values that can be computed from the argument: Then the floatingpoint result equals the value of the mathematical expression s·m·2^{e150}.int s = ((bits >> 31) == 0) ? 1 : 1; int e = ((bits >> 23) & 0xff); int m = (e == 0) ? (bits & 0x7fffff) << 1 : (bits & 0x7fffff)  0x800000; Note that this method may not be able to return a {@code float} NaN with exactly same bit pattern as the {@code int} argument. IEEE 754 distinguishes between two kinds of NaNs, quiet NaNs and signaling NaNs. The differences between the two kinds of NaN are generally not visible in Java. Arithmetic operations on signaling NaNs turn them into quiet NaNs with a different, but often similar, bit pattern. However, on some processors merely copying a signaling NaN also performs that conversion. In particular, copying a signaling NaN to return it to the calling method may perform this conversion. So {@code intBitsToFloat} may not be able to return a {@code float} with a signaling NaN bit pattern. Consequently, for some {@code int} values, {@code floatToRawIntBits(intBitsToFloat(start))} may not equal {@code start}. Moreover, which particular bit patterns represent signaling NaNs is platform dependent; although all NaN bit patterns, quiet or signaling, must be in the NaN range identified above.  
public int intValue(){ return (int)value; }
 
public boolean isInfinite(){ return isInfinite(value); }
 
public static boolean isInfinite(float v){ return (v == POSITIVE_INFINITY)  (v == NEGATIVE_INFINITY); }
 
public boolean isNaN(){ return isNaN(value); }
 
public static boolean isNaN(float v){ return (v != v); }
 
public long longValue(){ return (long)value; }
 
public static float parseFloat(String s) throws NumberFormatException{ return FloatingDecimal.readJavaFormatString(s).floatValue(); }
 
public short shortValue(){ return (short)value; }
 
public static String toHexString(float f){ if (Math.abs(f) < FloatConsts.MIN_NORMAL && f != 0.0f ) {// float subnormal // Adjust exponent to create subnormal double, then // replace subnormal double exponent with subnormal float // exponent String s = Double.toHexString(FpUtils.scalb((double)f, /* 1022+126 */ DoubleConsts.MIN_EXPONENT FloatConsts.MIN_EXPONENT)); return s.replaceFirst("p1022$", "p126"); } else // double string will be the same as float string return Double.toHexString(f); }
 
public String toString(){ return Float.toString(value); }
 
public static String toString(float f){ return new FloatingDecimal(f).toJavaFormatString(); }
To create localized string representations of a floatingpoint value, use subclasses of java.text.NumberFormat .  
public static Float valueOf(String s) throws NumberFormatException{ return new Float(FloatingDecimal.readJavaFormatString(s).floatValue()); }
If {@code s} is {@code null}, then a {@code NullPointerException} is thrown. Leading and trailing whitespace characters in {@code s} are ignored. Whitespace is removed as if by the String#trim method; that is, both ASCII space and control characters are removed. The rest of {@code s} should constitute a FloatValue as described by the lexical syntax rules: where Sign, FloatingPointLiteral, HexNumeral, HexDigits, SignedInteger and FloatTypeSuffix are as defined in the lexical structure sections of The Java™ Language Specification, except that underscores are not accepted between digits. If {@code s} does not have the form of a FloatValue, then a {@code NumberFormatException} is thrown. Otherwise, {@code s} is regarded as representing an exact decimal value in the usual "computerized scientific notation" or as an exact hexadecimal value; this exact numerical value is then conceptually converted to an "infinitely precise" binary value that is then rounded to type {@code float} by the usual roundtonearest rule of IEEE 754 floatingpoint arithmetic, which includes preserving the sign of a zero value. Note that the roundtonearest rule also implies overflow and underflow behaviour; if the exact value of {@code s} is large enough in magnitude (greater than or equal to (#MAX_VALUE + ulp(MAX_VALUE) /2), rounding to {@code float} will result in an infinity and if the exact value of {@code s} is small enough in magnitude (less than or equal to #MIN_VALUE /2), rounding to float will result in a zero. Finally, after rounding a {@code Float} object representing this {@code float} value is returned. To interpret localized string representations of a floatingpoint value, use subclasses of java.text.NumberFormat . Note that trailing format specifiers, specifiers that
determine the type of a floatingpoint literal
({@code 1.0f} is a {@code float} value;
{@code 1.0d} is a {@code double} value), do
not influence the results of this method. In other
words, the numerical value of the input string is converted
directly to the target floatingpoint type. In general, the
twostep sequence of conversions, string to {@code double}
followed by {@code double} to {@code float}, is
not equivalent to converting a string directly to
{@code float}. For example, if first converted to an
intermediate {@code double} and then to
{@code float}, the string To avoid calling this method on an invalid string and having a {@code NumberFormatException} be thrown, the documentation for Double.valueOf lists a regular expression which can be used to screen the input.  
public static Float valueOf(float f){ return new Float(f); }
