Java float type conversion
In this chapter you will learn:
- How to convert Float to byte, double, float, int, long and short
- How to convert float value to Hex String value
- How to convert float value to String value
- How to convert string value to float value
Convert Float to byte, double, float, int, long and short
It is possible to convert float type value to other primitive types.
byte byteValue()
returns the value of this Float as a byte (by casting to a byte).double doubleValue()
returns the double value of this Float object.float floatValue()
returns the float value of this Float object.int intValue()
returns the value of this Float as an int (by casting to type int).long longValue()
returns value of this Float as a long (by casting to type long).short shortValue()
returns the value of this Float as a short (by casting to a short).
The following code creates a float
type value and then converts
it to different primitive types.
public class Main {
public static void main(String[] args) {
Float floatObject = new Float("10.01");
byte b = floatObject.byteValue();
System.out.println("byte:"+b);
//j a v a2 s . co m
short s = floatObject.shortValue();
System.out.println("short:"+s);
int i = floatObject.intValue();
System.out.println("int:"+i);
float f = floatObject.floatValue();
System.out.println("float"+f);
double d = floatObject.doubleValue();
System.out.println("double:"+d);
long l = floatObject.longValue();
System.out.println("long:"+l);
}
}
The output:
Convert float value to Hex String value
static String toHexString(float f)
returns a hexadecimal string representation of the float argument.
public class Main {
public static void main(String[] args) {
Float floatObject2 = Float.valueOf(1.1F);
System.out.println(floatObject2);/*from j av a2 s . c o m*/
System.out.println(Float.toHexString(floatObject2));
}
}
The output:
Convert float value to String value
String toString()
returns a string representation of this Float object.static String toString(float f)
returns a string representation of the float argument.
public class Main {
public static void main(String[] args) {
Float floatObject2 = Float.valueOf(1.1F);
System.out.println(floatObject2.toString());
System.out.println(Float.toString(floatObject2));
/*from j a v a2 s .co m*/
}
}
The output:
Convert string value to float value
Using the following methods we can convert string value to float type value.
static float parseFloat(String s)
returns a new float initialized to the value represented by the specified String, as performed by the valueOf method of class Float.static Float valueOf(float f)
returns a Float instance representing the specified float value.static Float valueOf(String s)
returns a Float object holding the float value represented by the argument string s.
public class Main {
public static void main(String[] args) {
Float floatObject2 = Float.parseFloat("1.1F");
System.out.println(floatObject2);/* j a v a 2 s. com*/
}
}
You cannot use floating-point literal as Java think it is a double.
public class Main {
public static void main(String[] args) {
Float floatObject2 = Float.valueOf(1.1);
System.out.println(floatObject2);// j a v a 2 s . c om
}
}
When compiling it, the compile generates error message:
Next chapter...
What you will learn in the next chapter:
Home » Java Tutorial » Primitive Data Types