Java float type conversion

In this chapter you will learn:

  1. How to convert Float to byte, double, float, int, long and short
  2. How to convert float value to Hex String value
  3. How to convert float value to String value
  4. 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:

  1. How to find out the Maximum value and minimum value a float type can have
Home » Java Tutorial » Primitive Data Types

Introduction

    Java Primitive Data Types

Boolean

    Java boolean type
    Java boolean type conversion
    Convert string value to boolean
    Convert boolean to string

Char

    Java char type
    Compare two char values
    Change char case
    Java char conversion
    Java char value and its attributes

Byte

    Java byte type
    Convert Byte to String
    Convert String to byte
    Byte object constructor
    Byte's max and min value
    Compare two byte values
    Convert Byte to byte, double, float, int, long and short

Short

    Java short type
    Short min/max value and size
    Create Short object
    Compare short values
    Convert short to String
    Convert Short to primitive types
    Convert string to short
    Reverse bytes

Integer

    Java int type
    int max/min value
    Create Java integer
    Convert int to binary, hexadecimal and octal format
    Compare integer values
    Integer sign
    Convert string to int
    Convert int to primitive types
    Convert int to String
    int bit operations

Long

    Java long type
    Compare two long values
    Convert long to binary, hex and octal
    Convert long value to primitive types
    Convert String to long value
    Convert long to String

Float

    Java float type
    Java float type conversion
    Predefined value for float type
    Compare two float value

Double

    Java double type
    Deal with NaN double value
    Compare two double values
    Java double type creation and comparison
    Java double type conversion

Data Type Conversion

    Java Automatic Type Conversion and Casting
    Data type casting
    Java type promotion
    Autoboxing and auto-unboxing