RoundingMode.values() in Java

RoundingMode.values(): This method is available in java.math.RoundingMode class of Java.

Syntax:

RoundingMode[] java.math.RoundingMode.values()

This method returns the RoundingMode array (or list of RoundingMode constants).

Parameters: NA

Returns: It returns the list of the RoundingMode constants.

Exceptions: NA

Approach

Java

import java.math.RoundingMode;
import java.util.Arrays;

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

        System.out.println(Arrays.toString(RoundingMode.values()));
    }
}


Output:

[UP, DOWN, CEILING, FLOOR, HALF_UP, HALF_DOWN, HALF_EVEN, UNNECESSARY]


Some other methods of RoundingMode

RoundingMode.CEILINGRounding mode to round towards positive infinity.

compareTo()This method compares this enum with the specified object for order.

describeConstable()This method returns an enum descriptor Enum Desc for this instance if one can be constructed, or an empty Optional if one cannot be.

RoundingMode.DOWNRounding mode to round towards zero.

equals(Object) This method returns true if the specified object is equal to this enum constant.

RoundingMode.FLOORRounding mode to round towards negative infinity.

getClass()This method returns the runtime class of this Object.

getDeclaringClass()This method returns the Class object corresponding to this enum constant's enum type.

RoundingMode.HALF_DOWNRounding mode to round towards "nearest neighbor"unless both neighbors are equidistant, in which case round down.

RoundingMode.HALF_EVENRounding mode to round towards the "nearest neighbor" unless both neighbors are equidistant, in which case, round towards the even neighbor.

RoundingMode.HALF_UPRounding mode to round towards "nearest neighbor" unless both neighbors are equidistant, in which case round up.

hashCode()This method returns a hash code for this enum constant.

name()This method returns the name of this enum constant, exactly as declared in its enum declaration.

ordinal()This method returns the ordinal of this enumeration constant.

toString()This method returns the name of this enum constant, as contained in the declaration.

RoundingMode.UNNECESSARYRounding mode to assert that the requested operation has an exact result, hence no rounding is necessary.

RoundingMode.UPRounding mode to round away from zero.

RoundingMode.valueOf(int)This method returns the RoundingMode object corresponding to a legacy integer rounding mode constant in BigDecimal.

RoundingMode.valueOf(String)his method returns the RoundingMode object corresponding to a legacy String rounding mode constant in BigDecimal.

RoundingMode.values()This method returns the RoundingMode array (or list of RoundingMode constants).

RoundingMode.valueOf(String) in Java

RoundingMode.valueOf(String): This method is available in java.math.RoundingMode class of Java.

Syntax:

RoundingMode java.math.RoundingMode.valueOf(String name)

This method takes one argument of type String as its parameter. This method returns the RoundingMode object corresponding to a legacy String rounding mode constant in BigDecimal.

Parameters: One parameter is required for this method.

name: legacy String rounding mode to convert.

Returns: RoundingMode String constants.

Throws:

IllegalArgumentException - String is not an enum constant. 

Approach 1: When no exceptions.

Java

import java.math.RoundingMode;

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

        String name = "CEILING";
        System.out.println(RoundingMode.valueOf(name));
    }
}

Output:

CEILING


Approach 2: IllegalArgumentException 

Java

import java.math.RoundingMode;

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

        String name = "HELLO";
        System.out.println(RoundingMode.valueOf(name));
    }
}


Output:

Exception in thread "main" java.lang.IllegalArgumentException: No enum constant java.math.RoundingMode.HELLO at java.base/java.lang.Enum.valueOf(Enum.java:273) at java.base/java.math.RoundingMode.valueOf(RoundingMode.java:104)


Some other methods of RoundingMode

RoundingMode.CEILINGRounding mode to round towards positive infinity.

compareTo()This method compares this enum with the specified object for order.

describeConstable()This method returns an enum descriptor Enum Desc for this instance if one can be constructed, or an empty Optional if one cannot be.

RoundingMode.DOWNRounding mode to round towards zero.

equals(Object) This method returns true if the specified object is equal to this enum constant.

RoundingMode.FLOORRounding mode to round towards negative infinity.

getClass()This method returns the runtime class of this Object.

getDeclaringClass()This method returns the Class object corresponding to this enum constant's enum type.

RoundingMode.HALF_DOWNRounding mode to round towards "nearest neighbor"unless both neighbors are equidistant, in which case round down.

RoundingMode.HALF_EVENRounding mode to round towards the "nearest neighbor" unless both neighbors are equidistant, in which case, round towards the even neighbor.

RoundingMode.HALF_UPRounding mode to round towards "nearest neighbor" unless both neighbors are equidistant, in which case round up.

hashCode()This method returns a hash code for this enum constant.

name()This method returns the name of this enum constant, exactly as declared in its enum declaration.

ordinal()This method returns the ordinal of this enumeration constant.

toString()This method returns the name of this enum constant, as contained in the declaration.

RoundingMode.UNNECESSARYRounding mode to assert that the requested operation has an exact result, hence no rounding is necessary.

RoundingMode.UPRounding mode to round away from zero.

RoundingMode.valueOf(int)This method returns the RoundingMode object corresponding to a legacy integer rounding mode constant in BigDecimal.

RoundingMode.valueOf(String)his method returns the RoundingMode object corresponding to a legacy String rounding mode constant in BigDecimal.

RoundingMode.values()This method returns the RoundingMode array (or list of RoundingMode constants).

RoundingMode.valueOf(int) in Java

RoundingMode.valueOf(int): This method is available in java.math.RoundingMode class of Java.

Syntax:

RoundingMode java.math.RoundingMode.valueOf(int rm)

This method takes one argument of type int as its parameter. This method returns the RoundingMode object corresponding to alegacy integer rounding mode constant in BigDecimal.

Parameters: One parameter is required for this method.

rm: legacy integer rounding mode to convert.

Returns: RoundingMode corresponding to the given integer.

Throws:

IllegalArgumentException - integer is out of range.

Approach 1: When no exceptions.

Java

import java.math.RoundingMode;

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

        int rm = 0;
        System.out.println(RoundingMode.valueOf(rm));
    }
}

Output:

UP


Approach 2: IllegalArgumentException 

Java

import java.math.RoundingMode;

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

        int rm = 100;
        System.out.println(RoundingMode.valueOf(rm));
    }
}


Output:

Exception in thread "main" java.lang.IllegalArgumentException: argument out of range at java.base/java.math.RoundingMode.valueOf(RoundingMode.java:407)


Some other methods of RoundingMode

RoundingMode.CEILINGRounding mode to round towards positive infinity.

compareTo()This method compares this enum with the specified object for order.

describeConstable()This method returns an enum descriptor Enum Desc for this instance if one can be constructed, or an empty Optional if one cannot be.

RoundingMode.DOWNRounding mode to round towards zero.

equals(Object) This method returns true if the specified object is equal to this enum constant.

RoundingMode.FLOORRounding mode to round towards negative infinity.

getClass()This method returns the runtime class of this Object.

getDeclaringClass()This method returns the Class object corresponding to this enum constant's enum type.

RoundingMode.HALF_DOWNRounding mode to round towards "nearest neighbor"unless both neighbors are equidistant, in which case round down.

RoundingMode.HALF_EVENRounding mode to round towards the "nearest neighbor" unless both neighbors are equidistant, in which case, round towards the even neighbor.

RoundingMode.HALF_UPRounding mode to round towards "nearest neighbor" unless both neighbors are equidistant, in which case round up.

hashCode()This method returns a hash code for this enum constant.

name()This method returns the name of this enum constant, exactly as declared in its enum declaration.

ordinal()This method returns the ordinal of this enumeration constant.

toString()This method returns the name of this enum constant, as contained in the declaration.

RoundingMode.UNNECESSARYRounding mode to assert that the requested operation has an exact result, hence no rounding is necessary.

RoundingMode.UPRounding mode to round away from zero.

RoundingMode.valueOf(int)This method returns the RoundingMode object corresponding to a legacy integer rounding mode constant in BigDecimal.

RoundingMode.valueOf(String)his method returns the RoundingMode object corresponding to a legacy String rounding mode constant in BigDecimal.

RoundingMode.values()This method returns the RoundingMode array (or list of RoundingMode constants).

RoundingMode.UP in Java

RoundingMode.UP: This is available in java.math.RoundingMode class of Java.

Syntax:

java.math.RoundingMode.UP

Rounding mode to round away from zero.

Note: This rounding mode never decreases the magnitude of the calculated value.

Approach

Java

import java.math.RoundingMode;

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

        System.out.println(RoundingMode.UP);
    }
}

Output:

UP


Some other methods of RoundingMode

RoundingMode.CEILINGRounding mode to round towards positive infinity.

compareTo()This method compares this enum with the specified object for order.

describeConstable()This method returns an enum descriptor Enum Desc for this instance if one can be constructed, or an empty Optional if one cannot be.

RoundingMode.DOWNRounding mode to round towards zero.

equals(Object) This method returns true if the specified object is equal to this enum constant.

RoundingMode.FLOORRounding mode to round towards negative infinity.

getClass()This method returns the runtime class of this Object.

getDeclaringClass()This method returns the Class object corresponding to this enum constant's enum type.

RoundingMode.HALF_DOWNRounding mode to round towards "nearest neighbor"unless both neighbors are equidistant, in which case round down.

RoundingMode.HALF_EVENRounding mode to round towards the "nearest neighbor" unless both neighbors are equidistant, in which case, round towards the even neighbor.

RoundingMode.HALF_UPRounding mode to round towards "nearest neighbor" unless both neighbors are equidistant, in which case round up.

hashCode()This method returns a hash code for this enum constant.

name()This method returns the name of this enum constant, exactly as declared in its enum declaration.

ordinal()This method returns the ordinal of this enumeration constant.

toString()This method returns the name of this enum constant, as contained in the declaration.

RoundingMode.UNNECESSARYRounding mode to assert that the requested operation has an exact result, hence no rounding is necessary.

RoundingMode.UPRounding mode to round away from zero.

RoundingMode.valueOf(int)This method returns the RoundingMode object corresponding to a legacy integer rounding mode constant in BigDecimal.

RoundingMode.valueOf(String)his method returns the RoundingMode object corresponding to a legacy String rounding mode constant in BigDecimal.

RoundingMode.values()This method returns the RoundingMode array (or list of RoundingMode constants).

RoundingMode.UNNECESSARY in Java

RoundingMode.UNNECESSARY: This is available in java.math.RoundingMode class of Java.

Syntax:

java.math.RoundingMode.UNNECESSARY

Rounding mode to assert that the requested operation has an exact result, hence no rounding is necessary.

Approach

Java

import java.math.RoundingMode;

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

        System.out.println(RoundingMode.UNNECESSARY);
    }
}

Output:

UNNECESSARY


Some other methods of RoundingMode

RoundingMode.CEILINGRounding mode to round towards positive infinity.

compareTo()This method compares this enum with the specified object for order.

describeConstable()This method returns an enum descriptor Enum Desc for this instance if one can be constructed, or an empty Optional if one cannot be.

RoundingMode.DOWNRounding mode to round towards zero.

equals(Object) This method returns true if the specified object is equal to this enum constant.

RoundingMode.FLOORRounding mode to round towards negative infinity.

getClass()This method returns the runtime class of this Object.

getDeclaringClass()This method returns the Class object corresponding to this enum constant's enum type.

RoundingMode.HALF_DOWNRounding mode to round towards "nearest neighbor"unless both neighbors are equidistant, in which case round down.

RoundingMode.HALF_EVENRounding mode to round towards the "nearest neighbor" unless both neighbors are equidistant, in which case, round towards the even neighbor.

RoundingMode.HALF_UPRounding mode to round towards "nearest neighbor" unless both neighbors are equidistant, in which case round up.

hashCode()This method returns a hash code for this enum constant.

name()This method returns the name of this enum constant, exactly as declared in its enum declaration.

ordinal()This method returns the ordinal of this enumeration constant.

toString()This method returns the name of this enum constant, as contained in the declaration.

RoundingMode.UNNECESSARYRounding mode to assert that the requested operation has an exact result, hence no rounding is necessary.

RoundingMode.UPRounding mode to round away from zero.

RoundingMode.valueOf(int)This method returns the RoundingMode object corresponding to a legacy integer rounding mode constant in BigDecimal.

RoundingMode.valueOf(String)his method returns the RoundingMode object corresponding to a legacy String rounding mode constant in BigDecimal.

RoundingMode.values()This method returns the RoundingMode array (or list of RoundingMode constants).

RoundingMode toString() in Java

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

Syntax:

String java.lang.Enum.toString()

This method returns the name of this enum constant, as contained in the declaration.

Parameters: NA

Returns: the name of this enum constant.

Exceptions: NA

Approach

Java

import java.math.RoundingMode;

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

        RoundingMode rm = RoundingMode.CEILING;

        System.out.println(rm.toString());
    }
}

Output:

CEILING


Some other methods of RoundingMode

RoundingMode.CEILINGRounding mode to round towards positive infinity.

compareTo()This method compares this enum with the specified object for order.

describeConstable()This method returns an enum descriptor Enum Desc for this instance if one can be constructed, or an empty Optional if one cannot be.

RoundingMode.DOWNRounding mode to round towards zero.

equals(Object) This method returns true if the specified object is equal to this enum constant.

RoundingMode.FLOORRounding mode to round towards negative infinity.

getClass()This method returns the runtime class of this Object.

getDeclaringClass()This method returns the Class object corresponding to this enum constant's enum type.

RoundingMode.HALF_DOWNRounding mode to round towards "nearest neighbor"unless both neighbors are equidistant, in which case round down.

RoundingMode.HALF_EVENRounding mode to round towards the "nearest neighbor" unless both neighbors are equidistant, in which case, round towards the even neighbor.

RoundingMode.HALF_UPRounding mode to round towards "nearest neighbor" unless both neighbors are equidistant, in which case round up.

hashCode()This method returns a hash code for this enum constant.

name()This method returns the name of this enum constant, exactly as declared in its enum declaration.

ordinal()This method returns the ordinal of this enumeration constant.

toString()This method returns the name of this enum constant, as contained in the declaration.

RoundingMode.UNNECESSARYRounding mode to assert that the requested operation has an exact result, hence no rounding is necessary.

RoundingMode.UPRounding mode to round away from zero.

RoundingMode.valueOf(int)This method returns the RoundingMode object corresponding to a legacy integer rounding mode constant in BigDecimal.

RoundingMode.valueOf(String)his method returns the RoundingMode object corresponding to a legacy String rounding mode constant in BigDecimal.

RoundingMode.values()This method returns the RoundingMode array (or list of RoundingMode constants).

RoundingMode ordinal() in Java

ordinal(): This method is available in java.lang.Enum class of Java.

Syntax:

int java.lang.Enum.ordinal()

This method returns the ordinal of this enumeration constant.

Parameters: NA

Returns: the ordinal of this enumeration constant.

Exceptions: NA

Approach

Java

import java.math.RoundingMode;

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

        RoundingMode rm = RoundingMode.CEILING;

        System.out.println(rm.ordinal());
    }
}

Output:

2


Some other methods of RoundingMode

RoundingMode.CEILINGRounding mode to round towards positive infinity.

compareTo()This method compares this enum with the specified object for order.

describeConstable()This method returns an enum descriptor Enum Desc for this instance if one can be constructed, or an empty Optional if one cannot be.

RoundingMode.DOWNRounding mode to round towards zero.

equals(Object) This method returns true if the specified object is equal to this enum constant.

RoundingMode.FLOORRounding mode to round towards negative infinity.

getClass()This method returns the runtime class of this Object.

getDeclaringClass()This method returns the Class object corresponding to this enum constant's enum type.

RoundingMode.HALF_DOWNRounding mode to round towards "nearest neighbor"unless both neighbors are equidistant, in which case round down.

RoundingMode.HALF_EVENRounding mode to round towards the "nearest neighbor" unless both neighbors are equidistant, in which case, round towards the even neighbor.

RoundingMode.HALF_UPRounding mode to round towards "nearest neighbor" unless both neighbors are equidistant, in which case round up.

hashCode()This method returns a hash code for this enum constant.

name()This method returns the name of this enum constant, exactly as declared in its enum declaration.

ordinal()This method returns the ordinal of this enumeration constant.

toString()This method returns the name of this enum constant, as contained in the declaration.

RoundingMode.UNNECESSARYRounding mode to assert that the requested operation has an exact result, hence no rounding is necessary.

RoundingMode.UPRounding mode to round away from zero.

RoundingMode.valueOf(int)This method returns the RoundingMode object corresponding to a legacy integer rounding mode constant in BigDecimal.

RoundingMode.valueOf(String)his method returns the RoundingMode object corresponding to a legacy String rounding mode constant in BigDecimal.

RoundingMode.values()This method returns the RoundingMode array (or list of RoundingMode constants).