Java Program to convert Java Float to Numeric Primitive Data Types

Java 8Object Oriented ProgrammingProgramming

Let us first declare a Float object.

Float ob = new Float("29.35");

Now, let us convert it to numeric primitive data type short using shortValue()

short val1 = ob.shortValue();

In the same way, we can convert it to int using intValue() method.

short val1 = ob.intValue();

The following is an example that converts Float to other numeric primitive data types as well.

Example

 Live Demo

public class Demo {
   public static void main(String args[]) {
      Float ob = new Float("29.35");
      // numeric primitive data types
      short val1 = ob.shortValue();
      System.out.println(val1);
      int val2 = ob.intValue();
      System.out.println(val2);
      float val3 = ob.floatValue();
      System.out.println(val3);
      double val4 = ob.doubleValue();
      System.out.println(val4);
   }
}

Output

29
29
29.35
29.350000381469727
raja
Published on 10-Dec-2018 11:22:50
Advertisements