Java Program to convert Java Float to Numeric Primitive Data Types


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

karthikeya Boyini
karthikeya Boyini

I love programming (: That's all I know

Updated on: 26-Jun-2020

276 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements