How to Convert Float to String or String to Float in java with examples

Float and String are primitive data types used to store various values.

In applications, UI forms often include input text fields that accept floating-point values. To handle form submissions and manipulate these float values, it is necessary to convert between String and float in Java. This tutorial discusses various methods for performing this conversion.

float values example:

float f = (float) 4.8;
float f1 =8.56f;
float f2 =5.67F;

In Java, when you provide decimal data, such as the value 8.59, it is treated as a double type by default. To specify that it should be treated as a float value, you need to append an f or F to the decimal value.

A String is a sequence of characters enclosed in double-quotes and is represented by a single variable name.

How to Convert Float to String in Java with Examples?

There are several ways to convert a Float to a String in Java.

  • using toString() method

    For float primitive types, the Float class provides a static method - public static String toString(float f). You can use it as follows: Example

    float f =3.585f ;
    String s = Float.toString(f);
    

    For Float objects, you can call the toString() method directly on the object:

    Float f1 =4.85f ;
    System.out.println(f1.toString()); //outputs 4.85
    
  • Using String valueOf() method

    The String class has a valueOf() method, which is overloaded to accept a float value and return its string representation:

    float f = 4.89F;
    String ss = String.valueOf(f);
    System.out.println(ss);  // outputs 4.89
    

    These methods provide flexibility when converting float values to strings in Java.

How to Convert String to Float in Java with Examples?

Converting a String to a Float in Java can be achieved through various methods. Let’s explore one commonly used method:

  • using Float.parseFloat() method

    The parseFloat() method in the Float class takes a string as a value and converts it to a float. If the string cannot be converted, a NumberFormatException will be thrown.

    String s="45.5";
    float f=Float.parseFloat(s);
    System.out.println(f);
    String s1="45abc.5";
    
    try {
      float f1=Float.parseFloat(s1);
      System.out.println(f);
    } catch (NumberFormatException e) {
      System.out.println("Unable to convert the invalid string to float.");
    }
    

    Output is

    45.5
    Exception in thread "main" java.lang.NumberFormatException: For input string: "45abc.5"
    at java.base/jdk.internal.math.FloatingDecimal.readJavaFormatString(FloatingDecimal.java:2054)
    at java.base/jdk.internal.math.FloatingDecimal.parseFloat(FloatingDecimal.java:122)
    at java.base/java.lang.Float.parseFloat(Float.java:455)
    at ArraysDemo.main(ArraysDemo.java:29)
    

    In the example, the first conversion is successful, while the second one throws a NumberFormatException due to the presence of non-numeric characters in the string.

This method provides a straightforward way to convert a string containing a numeric value to a float.