Java : Float with Examples

Float (Java SE 18 & JDK 18) API Examples.
You will find code examples on most Float methods.


Summary

The Float class wraps a value of primitive type float in an object. An object of type Float contains a single field whose type is float.

Class diagram

// Compile error.
final var list = new ArrayList<float>();
// OK.
final var list = new ArrayList<Float>();
list.add(1.11f);
list.add(2.22f);
list.add(3.33f);

System.out.println(list); // [1.11, 2.22, 3.33]

Fields

static final int BYTES

The number of bytes used to represent a float value.

System.out.println(Float.BYTES); // 4

static final int MAX_EXPONENT

Maximum exponent a finite float variable may have.

System.out.println(Float.MAX_EXPONENT); // 127

static final float MAX_VALUE

A constant holding the largest positive finite value of type float, (2-2-23)·2127.

// 3.4028235E38
System.out.println(Float.MAX_VALUE);

// 340282346638528859811704183484516925440
System.out.println(new BigDecimal(Float.MAX_VALUE).toPlainString());

static final int MIN_EXPONENT

Minimum exponent a normalized float variable may have.

System.out.println(Float.MIN_EXPONENT); // -126

static final float MIN_NORMAL

A constant holding the smallest positive normal value of type float, 2-126.

// 1.17549435E-38
System.out.println(Float.MIN_NORMAL);

// 0.0000000000000000000000000000000000000117549435082228750796
// 873653722224567781866555677208752150875170627841725945472717
// 28515625
System.out.println(new BigDecimal(Float.MIN_NORMAL).toPlainString());

static final float MIN_VALUE

A constant holding the smallest positive nonzero value of type float, 2-149.

// 1.4E-45
System.out.println(Float.MIN_VALUE);

// 0.0000000000000000000000000000000000000000000014012984643248
// 170709237295832899161312802619418765157717570682838897910826
// 8586060148663818836212158203125
System.out.println(new BigDecimal(Float.MIN_VALUE).toPlainString());

static final float NaN

A constant holding a Not-a-Number (NaN) value of type float.

System.out.println(Float.NaN); // NaN

static final float NEGATIVE_INFINITY

A constant holding the negative infinity of type float.

System.out.println(Float.NEGATIVE_INFINITY); // -Infinity

static final float POSITIVE_INFINITY

A constant holding the positive infinity of type float.

System.out.println(Float.POSITIVE_INFINITY); // Infinity

static final int SIZE

The number of bits used to represent a float value.

System.out.println(Float.SIZE); // 32

static final Class<Float> TYPE

The Class instance representing the primitive type float.

System.out.println(Float.TYPE.getName()); // float
System.out.println(Float.TYPE.isPrimitive()); // true

Constructors

Float (double value)

Deprecated, for removal: This API element is subject to removal in a future version. It is rarely appropriate to use this constructor.

Deprecated. Use valueOf(float f) instead.

Float (float value)

Deprecated, for removal: This API element is subject to removal in a future version. It is rarely appropriate to use this constructor.

Deprecated. Use valueOf(float f) instead.

Float (String s)

Deprecated, for removal: This API element is subject to removal in a future version. It is rarely appropriate to use this constructor.

Deprecated. Use valueOf(String s) instead.

Methods

byte byteValue ()

Returns the value of this Float as a byte after a narrowing primitive conversion.

final byte value1 = Float.valueOf(100.0f).byteValue();
System.out.println(value1); // 100

final byte value2 = Float.valueOf(22.222f).byteValue();
System.out.println(value2); // 22

final byte value3 = Float.valueOf(9.99f).byteValue();
System.out.println(value3); // 9
System.out.println(Byte.MAX_VALUE); // 127
System.out.println(Byte.MIN_VALUE); // -128

final byte value1 = Float.valueOf(127.0f).byteValue();
System.out.println(value1); // 127

final byte value2 = Float.valueOf(128.0f).byteValue();
System.out.println(value2); // -128

final byte value3 = Float.valueOf(-128.0f).byteValue();
System.out.println(value3); // -128

final byte value4 = Float.valueOf(-129.0f).byteValue();
System.out.println(value4); // 127
final byte value1 = Float.valueOf(Float.NaN).byteValue();
System.out.println(value1); // 0

final byte value2 = Float.valueOf(Float.NEGATIVE_INFINITY).byteValue();
System.out.println(value2); // 0

final byte value3 = Float.valueOf(Float.POSITIVE_INFINITY).byteValue();
System.out.println(value3); // -1

static int compare (float f1, float f2)

Compares the two specified float values.

System.out.println(Float.compare(1.23f, 4.56f)); // -1
System.out.println(Float.compare(4.56f, 1.23f)); // 1

System.out.println(Float.compare(1.23f, -1.23f)); // 1
System.out.println(Float.compare(-1.23f, 1.23f)); // -1

System.out.println(Float.compare(0.0f, -0.0f)); // 1
System.out.println(Float.compare(-0.0f, 0.0f)); // -1
System.out.println(Float.compare(Float.NaN, 1.23f)); // 1
System.out.println(Float.compare(Float.NEGATIVE_INFINITY, 1.23f)); // -1
System.out.println(Float.compare(Float.POSITIVE_INFINITY, 1.23f)); // 1

int compareTo (Float anotherFloat)

Compares two Float objects numerically.

final var value1 = Float.valueOf(1.23f);
final var value2 = Float.valueOf(4.56f);

System.out.println(value1.compareTo(value2)); // -1
System.out.println(value2.compareTo(value1)); // 1
final var value1 = Float.valueOf(1.23f);
final var value2 = Float.valueOf(-1.23f);

System.out.println(value1.compareTo(value2)); // 1
System.out.println(value2.compareTo(value1)); // -1
final var value1 = Float.valueOf(0.0f);
final var value2 = Float.valueOf(-0.0f);

System.out.println(value1.compareTo(value2)); // 1
System.out.println(value2.compareTo(value1)); // -1
final var value = Float.valueOf(1.23f);

System.out.println(value.compareTo(Float.NaN)); // -1
System.out.println(value.compareTo(Float.NEGATIVE_INFINITY)); // 1
System.out.println(value.compareTo(Float.POSITIVE_INFINITY)); // -1

Optional<Float> describeConstable ()

Returns an Optional containing the nominal descriptor for this instance, which is the instance itself.

final var ret = Float.valueOf(1.23f).describeConstable();
System.out.println(ret); // Optional[1.23]

double doubleValue ()

Returns the value of this Float as a double after a widening primitive conversion.

final var ret1 = Float.valueOf(1.23f).doubleValue();
System.out.println(ret1); // 1.2300000190734863

final var ret2 = Float.valueOf(-4.56f).doubleValue();
System.out.println(ret2); // -4.559999942779541
System.out.println(Float.valueOf(Float.NaN).doubleValue()); // NaN
System.out.println(Float.valueOf(Float.NEGATIVE_INFINITY).doubleValue()); // -Infinity
System.out.println(Float.valueOf(Float.POSITIVE_INFINITY).doubleValue()); // Infinity

boolean equals (Object obj)

Compares this object against the specified object.

final var value1 = Float.valueOf(1.23f);
final var value2 = Float.valueOf(1.23f);

System.out.println(value1.equals(value2)); // true
final var value1 = Float.valueOf(1.23f);
final var value2 = Float.valueOf(4.56f);

System.out.println(value1.equals(value2)); // false
// An example with rounding error.
final var value1 = Float.valueOf(0.05f);
final var value2 = Float.valueOf(0.02f + 0.03f);

System.out.println(value1.equals(value2)); // false
final var value1 = Float.valueOf(Float.NaN);
final var value2 = Float.valueOf(Float.NaN);

System.out.println(value1.floatValue() == value2.floatValue()); // false
System.out.println(value1.equals(value2)); // true
final var value1 = Float.valueOf(+0.0f);
final var value2 = Float.valueOf(-0.0f);

System.out.println(value1.floatValue() == value2.floatValue()); // true
System.out.println(value1.equals(value2)); // false

static int floatToIntBits (float value)

Returns a representation of the specified floating-point value according to the IEEE 754 floating-point "single format" bit layout.

final var bits1 = Float.floatToIntBits(1.0f);
System.out.println(Integer.toHexString(bits1)); // 3f800000

final var bits2 = Float.floatToIntBits(-1.0f);
System.out.println(Integer.toHexString(bits2)); // bf800000
final var bits = Float.floatToIntBits(123.25f);
System.out.println(Integer.toHexString(bits)); // 42f68000
final var bits1 = Float.floatToIntBits(Float.NaN);
System.out.println(Integer.toHexString(bits1)); // 7fc00000

final var bits2 = Float.floatToIntBits(Float.NEGATIVE_INFINITY);
System.out.println(Integer.toHexString(bits2)); // ff800000

final var bits3 = Float.floatToIntBits(Float.POSITIVE_INFINITY);
System.out.println(Integer.toHexString(bits3)); // 7f800000

static int floatToRawIntBits (float value)

Returns a representation of the specified floating-point value according to the IEEE 754 floating-point "single format" bit layout, preserving Not-a-Number (NaN) values.

final var bits1 = Float.floatToRawIntBits(1.0f);
System.out.println(Integer.toHexString(bits1)); // 3f800000

final var bits2 = Float.floatToRawIntBits(-1.0f);
System.out.println(Integer.toHexString(bits2)); // bf800000
final var bits = Float.floatToRawIntBits(123.25f);
System.out.println(Integer.toHexString(bits)); // 42f68000
final var bits1 = Float.floatToRawIntBits(Float.NaN);
System.out.println(Integer.toHexString(bits1)); // 7fc00000

final var bits2 = Float.floatToRawIntBits(Float.NEGATIVE_INFINITY);
System.out.println(Integer.toHexString(bits2)); // ff800000

final var bits3 = Float.floatToRawIntBits(Float.POSITIVE_INFINITY);
System.out.println(Integer.toHexString(bits3)); // 7f800000

float floatValue ()

Returns the float value of this Float object.

final float ret1 = Float.valueOf(1.23f).floatValue();
System.out.println(ret1); // 1.23

final float ret2 = Float.valueOf(-4.56f).floatValue();
System.out.println(ret2); // -4.56
System.out.println(Float.valueOf(Float.NaN).floatValue()); // NaN
System.out.println(Float.valueOf(Float.NEGATIVE_INFINITY).floatValue()); // -Infinity
System.out.println(Float.valueOf(Float.POSITIVE_INFINITY).floatValue()); // Infinity

int hashCode ()

Returns a hash code for this Float object.

System.out.println(Float.valueOf(1.23f).hashCode()); // 1067282596
System.out.println(Float.valueOf(-4.56f).hashCode()); // -1064178811

System.out.println(Float.valueOf(Float.MAX_VALUE).hashCode()); // 2139095039
System.out.println(Float.valueOf(Float.MIN_VALUE).hashCode()); // 1

System.out.println(Float.valueOf(Float.NaN).hashCode()); // 2143289344
System.out.println(Float.valueOf(Float.NEGATIVE_INFINITY).hashCode()); // -8388608
System.out.println(Float.valueOf(Float.POSITIVE_INFINITY).hashCode()); // 2139095040

System.out.println(Float.valueOf(+0.0f).hashCode()); // 0
System.out.println(Float.valueOf(-0.0f).hashCode()); // -2147483648

static int hashCode (float value)

Returns a hash code for a float value; compatible with Float.hashCode().

System.out.println(Float.hashCode(1.23f)); // 1067282596
System.out.println(Float.hashCode(-4.56f)); // -1064178811

System.out.println(Float.hashCode(Float.MAX_VALUE)); // 2139095039
System.out.println(Float.hashCode(Float.MIN_VALUE)); // 1

System.out.println(Float.hashCode(Float.NaN)); // 2143289344
System.out.println(Float.hashCode(Float.NEGATIVE_INFINITY)); // -8388608
System.out.println(Float.hashCode(Float.POSITIVE_INFINITY)); // 2139095040

System.out.println(Float.hashCode(+0.0f)); // 0
System.out.println(Float.hashCode(-0.0f)); // -2147483648

static float intBitsToFloat (int bits)

Returns the float value corresponding to a given bit representation.

System.out.println(Float.intBitsToFloat(0x3f800000)); // 1.0
System.out.println(Float.intBitsToFloat(0xbf800000)); // -1.0

System.out.println(Float.intBitsToFloat(0x42f68000)); // 123.25

System.out.println(Float.intBitsToFloat(0x7fc00000)); // NaN
System.out.println(Float.intBitsToFloat(0xff800000)); // -Infinity
System.out.println(Float.intBitsToFloat(0x7f800000)); // Infinity

int intValue ()

Returns the value of this Float as an int after a narrowing primitive conversion.

final int value1 = Float.valueOf(100.0f).intValue();
System.out.println(value1); // 100

final int value2 = Float.valueOf(22.222f).intValue();
System.out.println(value2); // 22

final int value3 = Float.valueOf(9.99f).intValue();
System.out.println(value3); // 9
System.out.println(Integer.MAX_VALUE); // 2147483647
System.out.println(Integer.MIN_VALUE); // -2147483648

final int value1 = Float.valueOf(2147483647.0f).intValue();
System.out.println(value1); // 2147483647

final int value2 = Float.valueOf(2147483648.0f).intValue();
System.out.println(value2); // 2147483647

final int value3 = Float.valueOf(-2147483648.0f).intValue();
System.out.println(value3); // -2147483648

final int value4 = Float.valueOf(-2147483649.0f).intValue();
System.out.println(value4); // -2147483648
final int value1 = Float.valueOf(Float.NaN).intValue();
System.out.println(value1); // 0

final int value2 = Float.valueOf(Float.NEGATIVE_INFINITY).intValue();
System.out.println(value2); // -2147483648

final int value3 = Float.valueOf(Float.POSITIVE_INFINITY).intValue();
System.out.println(value3); // 2147483647

static boolean isFinite (float f)

Returns true if the argument is a finite floating-point value; returns false otherwise (for NaN and infinity arguments).

System.out.println(Float.isFinite(0.0f)); // true
System.out.println(Float.isFinite(1.23f)); // true
System.out.println(Float.isFinite(-4.56f)); // true

System.out.println(Float.isFinite(Float.NaN)); // false
System.out.println(Float.isFinite(Float.NEGATIVE_INFINITY)); // false
System.out.println(Float.isFinite(Float.POSITIVE_INFINITY)); // false

boolean isInfinite ()

Returns true if this Float value is infinitely large in magnitude, false otherwise.

System.out.println(Float.valueOf(0.0f).isInfinite()); // false
System.out.println(Float.valueOf(1.23f).isInfinite()); // false
System.out.println(Float.valueOf(-4.56f).isInfinite()); // false

System.out.println(Float.valueOf(Float.NaN).isInfinite()); // false
System.out.println(Float.valueOf(Float.NEGATIVE_INFINITY).isInfinite()); // true
System.out.println(Float.valueOf(Float.POSITIVE_INFINITY).isInfinite()); // true

static boolean isInfinite (float v)

Returns true if the specified number is infinitely large in magnitude, false otherwise.

System.out.println(Float.isInfinite(0.0f)); // false
System.out.println(Float.isInfinite(1.23f)); // false
System.out.println(Float.isInfinite(-4.56f)); // false

System.out.println(Float.isInfinite(Float.NaN)); // false
System.out.println(Float.isInfinite(Float.NEGATIVE_INFINITY)); // true
System.out.println(Float.isInfinite(Float.POSITIVE_INFINITY)); // true

boolean isNaN ()

Returns true if this Float value is a Not-a-Number (NaN), false otherwise.

System.out.println(Float.valueOf(0.0f).isNaN()); // false
System.out.println(Float.valueOf(1.23f).isNaN()); // false
System.out.println(Float.valueOf(-4.56f).isNaN()); // false

System.out.println(Float.valueOf(Float.NaN).isNaN()); // true
System.out.println(Float.valueOf(Float.NEGATIVE_INFINITY).isNaN()); // false
System.out.println(Float.valueOf(Float.POSITIVE_INFINITY).isNaN()); // false

static boolean isNaN (float v)

Returns true if the specified number is a Not-a-Number (NaN) value, false otherwise.

System.out.println(Float.isNaN(0.0f)); // false
System.out.println(Float.isNaN(1.23f)); // false
System.out.println(Float.isNaN(-4.56f)); // false

System.out.println(Float.isNaN(Float.NaN)); // true
System.out.println(Float.isNaN(Float.NEGATIVE_INFINITY)); // false
System.out.println(Float.isNaN(Float.POSITIVE_INFINITY)); // false

long longValue ()

Returns value of this Float as a long after a narrowing primitive conversion.

final long value1 = Float.valueOf(100.0f).longValue();
System.out.println(value1); // 100

final long value2 = Float.valueOf(22.222f).longValue();
System.out.println(value2); // 22

final long value3 = Float.valueOf(9.99f).longValue();
System.out.println(value3); // 9
System.out.println(Long.MAX_VALUE); // 9223372036854775807
System.out.println(Long.MIN_VALUE); // -9223372036854775808

final long value1 = Float.valueOf(9223372036854775807.0f).longValue();
System.out.println(value1); // 9223372036854775807

final long value2 = Float.valueOf(9223372036854775808.0f).longValue();
System.out.println(value2); // 9223372036854775807

final long value3 = Float.valueOf(-9223372036854775808.0f).longValue();
System.out.println(value3); // -9223372036854775808

final long value4 = Float.valueOf(-9223372036854775809.0f).longValue();
System.out.println(value4); // -9223372036854775808
final long value1 = Float.valueOf(Float.NaN).longValue();
System.out.println(value1); // 0

final long value2 = Float.valueOf(Float.NEGATIVE_INFINITY).longValue();
System.out.println(value2); // -9223372036854775808

final long value3 = Float.valueOf(Float.POSITIVE_INFINITY).longValue();
System.out.println(value3); // 9223372036854775807

static float max (float a, float b)

Returns the greater of two float values as if by calling Math.max.

System.out.println(Float.max(1.23f, 4.56f)); // 4.56
System.out.println(Float.max(-7.89f, 7.89f)); // 7.89

System.out.println(Float.max(1.23f, Float.NaN)); // NaN
System.out.println(Float.max(1.23f, Float.NEGATIVE_INFINITY)); // 1.23
System.out.println(Float.max(1.23f, Float.POSITIVE_INFINITY)); // Infinity

System.out.println(Float.max(+0.0f, -0.0f)); // 0.0

static float min (float a, float b)

Returns the smaller of two float values as if by calling Math.min.

System.out.println(Float.min(1.23f, 4.56f)); // 1.23
System.out.println(Float.min(-7.89f, 7.89f)); // -7.89

System.out.println(Float.min(1.23f, Float.NaN)); // NaN
System.out.println(Float.min(1.23f, Float.NEGATIVE_INFINITY)); // -Infinity
System.out.println(Float.min(1.23f, Float.POSITIVE_INFINITY)); // 1.23

System.out.println(Float.min(+0.0f, -0.0f)); // -0.0

static float parseFloat (String s)

Returns a new float initialized to the value represented by the specified String, as performed by the valueOf method of class Float.

final var ret1 = Float.parseFloat("1.23");
System.out.println(ret1); // 1.23

final var ret2 = Float.parseFloat("-4.56");
System.out.println(ret2); // -4.56

final var ret3 = Float.parseFloat("1.234e+3");
System.out.println(ret3); // 1234.0

final var ret4 = Float.parseFloat("1.234e-3");
System.out.println(ret4); // 0.001234

final var ret5 = Float.parseFloat("0xffffp-1");
System.out.println(ret5); // 32767.5
System.out.println(Float.toHexString(ret5)); // 0x1.fffep14
final var ret1 = Float.parseFloat("NaN");
System.out.println(ret1); // NaN

final var ret2 = Float.parseFloat("-Infinity");
System.out.println(ret2); // -Infinity

final var ret3 = Float.parseFloat("Infinity");
System.out.println(ret3); // Infinity

Float resolveConstantDesc (MethodHandles.Lookup lookup)

Resolves this instance as a ConstantDesc, the result of which is the instance itself.

final var value = Float.valueOf(1.23f);

final var ret = value.resolveConstantDesc(MethodHandles.lookup());
System.out.println(ret); // 1.23

short shortValue ()

Returns the value of this Float as a short after a narrowing primitive conversion.

final var value1 = Float.valueOf(100.0f).shortValue();
System.out.println(value1); // 100

final var value2 = Float.valueOf(22.222f).shortValue();
System.out.println(value2); // 22

final var value3 = Float.valueOf(9.99f).shortValue();
System.out.println(value3); // 9
System.out.println(Short.MAX_VALUE); // 32767
System.out.println(Short.MIN_VALUE); // -32768

final var value1 = Float.valueOf(32767.0f).shortValue();
System.out.println(value1); // 32767

final var value2 = Float.valueOf(32768.0f).shortValue();
System.out.println(value2); // -32768

final var value3 = Float.valueOf(-32768.0f).shortValue();
System.out.println(value3); // -32768

final var value4 = Float.valueOf(-32769.0f).shortValue();
System.out.println(value4); // 32767
final var value1 = Float.valueOf(Float.NaN).shortValue();
System.out.println(value1); // 0

final var value2 = Float.valueOf(Float.NEGATIVE_INFINITY).shortValue();
System.out.println(value2); // 0

final var value3 = Float.valueOf(Float.POSITIVE_INFINITY).shortValue();
System.out.println(value3); // -1

static float sum (float a, float b)

Adds two float values together as per the + operator.

System.out.println(Float.sum(1.0f, 5.0f)); // 6.0
System.out.println(Float.sum(1.0f, -5.0f)); // -4.0

System.out.println(Float.sum(1.234f, Float.NaN)); // NaN
System.out.println(Float.sum(1.234f, Float.NEGATIVE_INFINITY)); // -Infinity
System.out.println(Float.sum(1.234f, Float.POSITIVE_INFINITY)); // Infinity

// An example with rounding error.
System.out.println(Float.sum(0.02f, 0.03f)); // 0.049999997

static String toHexString (float f)

Returns a hexadecimal string representation of the float argument.

System.out.println(Float.toHexString(1.0f)); // 0x1.0p0
System.out.println(Float.toHexString(-1.0f)); // -0x1.0p0
System.out.println(Float.toHexString(2.0f)); // 0x1.0p1
System.out.println(Float.toHexString(3.0f)); // 0x1.8p1
System.out.println(Float.toHexString(0.5f)); // 0x1.0p-1
System.out.println(Float.toHexString(0.25f)); // 0x1.0p-2

System.out.println(Float.toHexString(Float.MAX_VALUE)); // 0x1.fffffep127
System.out.println(Float.toHexString(Float.MIN_VALUE)); // 0x0.000002p-126

System.out.println(Float.toHexString(Float.NaN)); // NaN
System.out.println(Float.toHexString(Float.NEGATIVE_INFINITY)); // -Infinity
System.out.println(Float.toHexString(Float.POSITIVE_INFINITY)); // Infinity

String toString ()

Returns a string representation of this Float object.

System.out.println(Float.valueOf(1.23f).toString()); // 1.23
System.out.println(Float.valueOf(-4.56f).toString()); // -4.56

System.out.println(Float.valueOf(1234567890.0f).toString()); // 1.23456794E9
System.out.println(Float.valueOf(1.23e-10f).toString()); // 1.23E-10

System.out.println(Float.valueOf(0.0f).toString()); // 0.0
System.out.println(Float.valueOf(-0.0f).toString()); // -0.0

System.out.println(Float.valueOf(Float.MAX_VALUE).toString()); // 3.4028235E38
System.out.println(Float.valueOf(Float.MIN_VALUE).toString()); // 1.4E-45

System.out.println(Float.valueOf(Float.NaN).toString()); // NaN
System.out.println(Float.valueOf(Float.NEGATIVE_INFINITY).toString()); // -Infinity
System.out.println(Float.valueOf(Float.POSITIVE_INFINITY).toString()); // Infinity

static String toString (float f)

Returns a string representation of the float argument.

System.out.println(Float.toString(1.23f)); // 1.23
System.out.println(Float.toString(-4.56f)); // -4.56

System.out.println(Float.toString(1234567890.0f)); // 1.23456794E9
System.out.println(Float.toString(1.23e-10f)); // 1.23E-10

System.out.println(Float.toString(0.0f)); // 0.0
System.out.println(Float.toString(-0.0f)); // -0.0

System.out.println(Float.toString(Float.MAX_VALUE)); // 3.4028235E38
System.out.println(Float.toString(Float.MIN_VALUE)); // 1.4E-45

System.out.println(Float.toString(Float.NaN)); // NaN
System.out.println(Float.toString(Float.NEGATIVE_INFINITY)); // -Infinity
System.out.println(Float.toString(Float.POSITIVE_INFINITY)); // Infinity

static Float valueOf (float f)

Returns a Float instance representing the specified float value.

final var value1 = Float.valueOf(1.23f);
System.out.println(value1); // 1.23

final var value2 = Float.valueOf(-4.56f);
System.out.println(value2); // -4.56

final var value3 = Float.valueOf(1234567890.0f);
System.out.println(value3); // 1.23456794E9

final var value4 = Float.valueOf(1.23e-10f);
System.out.println(value4); // 1.23E-10

final var value5 = Float.valueOf(Float.MAX_VALUE);
System.out.println(value5); // 3.4028235E38

final var value6 = Float.valueOf(Float.MIN_VALUE);
System.out.println(value6); // 1.4E-45

static Float valueOf (String s)

Returns a Float object holding the float value represented by the argument string s.

final var value1 = Float.valueOf("1.23");
System.out.println(value1); // 1.23

final var value2 = Float.valueOf("-4.56");
System.out.println(value2); // -4.56

final var value3 = Float.valueOf("1.234e+3");
System.out.println(value3); // 1234.0

final var value4 = Float.valueOf("1.234e-3");
System.out.println(value4); // 0.001234

final var value5 = Float.valueOf("0xffffp-1");
System.out.println(value5); // 32767.5
System.out.println(Float.toHexString(value5)); // 0x1.fffep14
final var value1 = Float.valueOf("NaN");
System.out.println(value1); // NaN

final var value2 = Float.valueOf("-Infinity");
System.out.println(value2); // -Infinity

final var value3 = Float.valueOf("Infinity");
System.out.println(value3); // Infinity

Related posts

To top of page