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);
//from j av a  2s.com
    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 ja  v  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  ja va2  s.c  om
  }
}

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);//from   j  a v  a 2 s  . c  om
    
  }
}

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  va 2s  .c  o m*/
    
  }
}

When compiling it, the compile generates error message:

Next chapter...

What you will learn in the next chapter:

  1. What is Java double type
  2. How to create double type Literals
  3. How to get the value of double infinity
  4. What is double type NaN(Not a Number)
Home » Java Tutorial » Data Types

Java Primitive Data Types
Java byte type
Java byte type conversion
Java short type
Java short type conversion
Java int type
Java int type conversion
Java long type
Java long type conversion
Java float type
Java float type conversion
Java double type
Java double type creation and comparison
Java double type conversion
Java Automatic Type Conversion and Casting
Data type casting
Java type promotion
Java char type
Java char conversion
Java char value and its attributes
Java boolean type
Java boolean type conversion
Autoboxing and auto-unboxing
Java Array
Create an Array
Array Index and length
Multidimensional Arrays
Array examples
Array copy
Array compare
Array Binary search
Array sort
Array to List
Array fill value
Array to String
BigInteger class
BigInteger creation
BigInteger add, subtract, multiply and divide
BigInteger power and modPow
BigInteger conversion
BigInteger to String
BigInteger bit and,or,xor,test,flip,negate
BigInteger bit shift left and right
BigInteger prime value
BigDecimal
BigDecimal constants
BigDecimal Rounding mode
BigDecimal creation
BigDecimal calculation
BigDecimal convert
BigDecimal Comparison
BigDecimal to String
BigDecimal decimal point
BigDecimal precision
BigDecimal format
Currency class