Showing posts with label Float Class. Show all posts
Showing posts with label Float Class. Show all posts

Float class Methods in Java

java.lang.Float

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. In addition, this class provides several methods for converting a float to a String and a String to the float, as well as other constants and methods useful when dealing with the float.

Some methods of Float class

Float.BYTESThe number of bytes is used to represent a float value (i.e. 4).


Float.compare(): It compares the two specified float values.


Float.floatToIntBits(): It returns a representation of the specified floating-point value according to the IEEE 754 floating-point "single format" bit layout.


Float.floatToRawIntBits()It 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.


Float.hashCode()It returns a hash code for a float value.


Float.intBitsToFloat()It returns the float value corresponding to a given bit representation.


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


Float.isInfinite() It returns true if the specified number is infinitely large in magnitude, false otherwise.


Float.isNaN()It returns true if the specified number is a Not-a-Number (NaN) value, false otherwise.


Float.max()It returns the greater of two float values.


Float.MAX_EXPONENTMaximum exponent a finite float variable may have.


Float.MAX_VALUEA constant holding the largest positive finite value of type float (i.e 3.4028235E38).


Float.min()It returns the smaller of two float values.


Float.MIN_EXPONENTMinimum exponent a normalized float variable may have (i.e. -126).


Float.MIN_NORMALA constant holding the smallest positive normal value of type float (i.e. 1.17549435E-38).


Float.NaNA constant holding a Not-a-Number (NaN) value of type float (i.e. NaN)


Float.NEGATIVE_INFINITYA constant holding the negative infinity of type float (i.e -Infinity).


Float.parseFloat()It returns a new float initialized to the value represented by the specified String, as performed by the valueOf method of class Float.


Float.POSITIVE_INFINITYA constant holding the positive infinity of type float (i.e. Infinity).


Float.SIZEThe number of bits used to represent a float value (i.e 32).


Float.sum()It adds two float values together as per the + operator.

Float.toHexString()It returns a hexadecimal string representation of the float argument.


Float.toString()It returns a string representation of the float argument.


Float.valueOf(float f)It returns a Float instance representing the specified float value.


Float.valueOf(String s)It returns a Float object holding the float value represented by the argument string s.



Float.valueOf() String in Java

Float.valueOf(): This method is available in java.lang.Float class of Java.

Syntax:

Float java.lang.Float.valueOf(String s) throws NumberFormatException

This method takes one argument of type String as its parameter. This method returns a Float object holding the float value represented by the argument string s.

Note: If s is null, then a NullPointerException is thrown.

Parameters: One parameter is required for this method.

s: the string to be parsed.

Returns: a Float object holding the value represented by the String argument.

Throws: NumberFormatException - if the string does not contain a parsable number.

For Example:

String s = "1818.56"

Float.valueOf(s) = > It returns 1818.56 

Approach

Java

public class FloatValueofString {
    public static void main(String[] args) {

        String s = "1818.56";
        System.out.println(Float.valueOf(s));
    }
}

Output:

1818.56

Float.valueOf() in Java

Float.valueOf(): This method is available in java.lang.Float class of Java.

Syntax:

Float java.lang.Float.valueOf(float f)

This method takes one argument of type float as its parameter. This method returns a Float instance representing the specified float value.

Parameters: One parameter is required for this method.

f: a float value.

Returns: a Float instance representing f.

For Example:

float f = 188.7f

Float.valueOf(f) = > It returns 188.7

Approach

Java

public class FloatValueofFloat {
    public static void main(String[] args) {

        float f = 188.7f;
        System.out.println(Float.valueOf(f));

    }
}

Output:

188.7

Float.toString() in Java

Float.toString(): This method is available in java.lang.Float class of Java.

Syntax:

String java.lang.Float.toString(float f)

This method takes one argument of type float as its parameter. This method returns a string representation of the float argument.

Note:

1. If the argument is NaN, the result is the string"NaN".

2. Otherwise, the result is a string that represents the sign and magnitude (absolute value) of the argument.

Parameters: One parameter is required for this method.

f: the float to be converted.

Returns: a string representation of the argument.

For Example:

float f = 1919.5f

Float.toString(f) = > It returns 1919.5

Approach

Java

public class FloattoString {
    public static void main(String[] args) {

        float f = 1919.5f;
        System.out.println(Float.toString(f));

    }
}

Output:

1919.5

Float.toHexString() in Java

Float.toHexString(): This method is available in java.lang.Float class of Java.

Syntax:

String java.lang.Float.toHexString(float f)

This method takes one argument of type float as its parameter. This method returns a hexadecimal string representation of the float argument.

Note:

1. If the argument is NaN, the result is the string"NaN".

2. Otherwise, the result is a string that represents the sign and magnitude (absolute value) of the argument.

Parameters: One parameter is required for this method.

f: the float to be converted.

Returns: a hex string representation of the argument.

For Example:

float f = 19919.6f

Float.toHexString(f) = > It returns 0x1.373e66p14

Approach

Java

public class FloattoHexString {
    public static void main(String[] args) {

        float f = 19919.6f;
        System.out.println(Float.toHexString(f));

    }
}

Output:

0x1.373e66p14

Float.sum() in Java

Float.sum(): This method is available in java.lang.Float class of Java.

Syntax:

float java.lang.Float.sum(float a, float b)

This method takes two arguments of type float as its parameters. This method adds two float values together as per the + operator.

Parameters: Two parameters are required for this method.

a: the first operand.

b: the second operand.

Returns: The sum of a and b.

Approach

Java

public class FloatSum {
    public static void main(String[] args) {

        float a = 1919.6f, b = 9891.7f;
        System.out.println(Float.sum(a, b));

    }
}

Output:

11811.3

Float.SIZE in Java

Float.SIZE: This is available in java.lang.Float class of Java.

Syntax:

int java.lang.Float.SIZE

The number of bits used to represent a float value (i.e 32).

Approach

Java

public class FloatSize {
    public static void main(String[] args) {

        System.out.println(Float.SIZE);

    }
}

Output:

32

Float.POSITIVE_INFINITY in Java

Float.POSITIVE_INFINITY: This is available in java.lang.Float class of Java.

Syntax:

float java.lang.Float.POSITIVE_INFINITY

A constant holding the positive infinity of type float (i.e. Infinity).

Approach

Java

public class FloatPositiveInfinity {
    public static void main(String[] args) {

        System.out.println(Float.POSITIVE_INFINITY);

    }
}

Output:

Infinity

Float.parseFloat() in Java

Float.parseFloat(): This method is available in java.lang.Float class of Java.

Syntax:

float java.lang.Float.parseFloat(String s) throws NumberFormatException

This method takes one argument of type String as its parameter. This method returns a new float initialized to the value represented by the specified String, as performed by the valueOf method of class Float.

Parameters: One parameter is required for this method.

s: the string to be parsed.

Returns: the float value represented by the string argument.

Throws: NullPointerException - if the string is nullNumberFormatException - if the string does not contain a parsable float.

For Example:

String s = "1818.99"

Float.parseFloat(s) = > It returns 1818.99

Approach

Java

public class FloatparseFloat {
    public static void main(String[] args) {

        String s = "1818.99";
        System.out.println(Float.parseFloat(s));

    }
}

Output:

1818.99

Float.NEGATIVE_INFINITY in Java

Float.NEGATIVE_INFINITY: This is available in java.lang.Float class of Java.

Syntax:

float java.lang.Float.NEGATIVE_INFINITY

A constant holding the negative infinity of type float (i.e -Infinity).

Approach

Java

public class FloatNegativeInfinity {
    public static void main(String[] args) {

        System.out.println(Float.NEGATIVE_INFINITY);

    }
}

Output:

-Infinity

Float.NaN in Java

Float.NaN: This is available in java.lang.Float class of Java.

Syntax:

float java.lang.Float.NaN

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

Approach

Java

public class FloatNaN {
    public static void main(String[] args) {

        System.out.println(Float.NaN);

    }
}

Output:

NaN

Float.MIN_NORMAL in Java

Float.MIN_NORMAL: This is available in java.lang.Float class of Java.

Syntax:

float java.lang.Float.MIN_NORMAL

A constant holding the smallest positive normal value of type float (i.e. 1.17549435E-38).

Approach

Java

public class FloatMinNormal {
    public static void main(String[] args) {

        System.out.println(Float.MIN_NORMAL);

    }
}

Output:

1.17549435E-38

Float.MIN_EXPONENT in Java

Float.MIN_EXPONENT: This is available in java.lang.Float class of Java.

Syntax:

int java.lang.Float.MIN_EXPONENT

Minimum exponent a normalized float variable may have (i.e. -126).

Approach

Java

public class FloatMinExponent {
    public static void main(String[] args) {

        System.out.println(Float.MIN_EXPONENT);

    }
}

Output:

-126

Float.min() in Java

Float.min(): This method is available in java.lang.Float class of Java.

Syntax:

float java.lang.Float.min(float a, float b)

This method takes two arguments of type float as its parameters. This method returns the smaller of two float values.

Parameters: Two parameters are required for this method.

a: the first operand.

b: the second operand.

Returns: the smaller of a and b.

Approach

Java

public class FloatMin {
    public static void main(String[] args) {

        float a = 1919.56f, b = 1991.5f;
        System.out.println(Float.min(a, b));

    }
}

Output:

1919.56

Float.MAX_VALUE in Java

Float.MAX_VALUE: This is available in java.lang.Float class of Java.

Syntax:

float java.lang.Float.MAX_VALUE

A constant holding the largest positive finite value of type float (i.e 3.4028235E38).

Approach

Java

public class FloatMaxValue {
    public static void main(String[] args) {

        System.out.println(Float.MAX_VALUE);

    }
}

Output:

3.4028235E38

Float.MAX_EXPONENT in Java

Float.MAX_EXPONENT: This method is available in java.lang.Float class of Java.

Syntax:

int java.lang.Float.MAX_EXPONENT 

Maximum exponent a finite float variable may have.

Approach

Java

public class FloatMaxExponent {
    public static void main(String[] args) {

        System.out.println(Float.MAX_EXPONENT);

    }
}

Output:

127

Float.max() in Java

Float.max(): This method is available in java.lang.Float class of Java.

Syntax:

float java.lang.Float.max(float a, float b)

This method takes two arguments of type float as its parameters. This method returns the greater of two float values.

Parameters: Two parameters are required for this method.

a: the first operand.

b: the second operand.

Returns: the greater of a and b.

For Example:

float a = 1991.5f, b = 1991.119f

Float.max(a,b) = > It returns 1991.5

Approach

Java

public class FloatMax {
    public static void main(String[] args) {

        float a = 1991.5f, b = 1991.119f;
        System.out.println(Float.max(a, b));

    }
}

Output:

1991.5

Float.isNaN() in Java

Float.isNaN(): This method is available in java.lang.Float class of Java.

Syntax:

boolean java.lang.Float.isNaN(float v)

This method takes one argument of type float as its parameter. This method returns true if the specified number is a Not-a-Number (NaN) value, false otherwise.

Parameters: One parameter is required for this method.

v: the value to be tested.

Returns: true if the argument is NaN; false otherwise.

For Example:

float v = 177.9f

Float.isNaN(v) = > It returns false. 

Approach

Java

public class FloatisNaN {
    public static void main(String[] args) {

        float v = 177.9f;
        System.out.println(Float.isNaN(v));

    }
}

Output:

false

Float.isInfinite() in Java

Float.isInfinite(): This method is available in java.lang.Float class of Java.

Syntax:

boolean java.lang.Float.isInfinite(float v)

This method takes one argument of type float as its parameter. This method returns true if the specified number is infinitely large in magnitude, false otherwise.

Parameters: One parameter is required for this method.

v: the value to be tested.

Returns: true if the argument is positive infinity or negative infinity; false otherwise

For Example:

float v = 1919.9f

Float.isInfinite(v) = > It returns false.

Approach

Java

public class FloatisInfinite {
    public static void main(String[] args) {

        float v = 1919.9f;
        System.out.println(Float.isInfinite(v));

    }
}

Output:

false

Float.isFinite() in Java

Float.isFinite(): This method is available in java.lang.Float class of Java.

Syntax:

boolean java.lang.Float.isFinite(float f)

This method takes one argument of type float as its parameter. This method returns true if the argument is a finite floating-point value; returns false otherwise (for NaN and infinity arguments).

Parameters: One parameter is required for this method.

f: the float value to be tested.

Returns: true if the argument is a finite floating-point value, false otherwise.

For Example:

float f = (float) 199191.198

Float.isFinite(f) = > It returns true.

Approach

Java

public class FloatIsFinite {
    public static void main(String[] args) {

        float f = (float199191.198;
        System.out.println(Float.isFinite(f));

    }
}

Output:

true