How to Convert a Float Type to Another Data Type in Java


In this example, we will convert a Float type into numeric primitive data types – Convert Float to byte – Convert Float to short – Convert Float to int – Convert Float to float – Convert Float to double.

Source Code

package com.beginner.examples;

public class ConvertFloatExample {

	public static void main(String[] args) {

		Float float1 = new Float(Math.PI);

		// Convert to byte type
		byte toByte = float1.byteValue();

		// Convert to short type
		short toShort = float1.shortValue();

		// Convert to int type
		int toInt = float1.intValue();

		// Convert to double type
		double toDouble = float1.doubleValue();

		
		System.out.println("Byte:" + toByte);
		
		System.out.println("Short:" + toShort);
		
		System.out.println("Int:" + toInt);
		
		System.out.println("Float:" + float1);
		
		System.out.println("Double:" + toDouble);

	}

}

Output:

Byte:3
Short:3
Int:3
Float:3.1415927
Double:3.1415927410125732
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments