Convert String To Float in Java

Introduction

Converting a String to a Float in Java is a common task that can be useful in various scenarios, such as parsing user input, reading data from files, or handling configuration parameters. In Java, this conversion can be done using several methods. This blog post will explore different methods to convert a String to a Float in Java.

Table of Contents

  1. Using Float.parseFloat()
  2. Using Float.valueOf()
  3. Using DecimalFormat
  4. Complete Example Program
  5. Conclusion

1. Using Float.parseFloat()

The Float.parseFloat() method parses the string argument as a float.

Example:

public class StringToFloatUsingParseFloat {
    public static void main(String[] args) {
        String strValue = "123.45";

        // Convert string to float using Float.parseFloat()
        float floatValue = Float.parseFloat(strValue);

        System.out.println("String value: " + strValue);
        System.out.println("Float value: " + floatValue);
    }
}

Output:

String value: 123.45
Float value: 123.45

Explanation:

  • Float.parseFloat(strValue) converts the string value to its float representation.

2. Using Float.valueOf()

The Float.valueOf() method returns a Float instance representing the specified string value. This method can also be used to convert a String to a float.

Example:

public class StringToFloatUsingValueOf {
    public static void main(String[] args) {
        String strValue = "678.90";

        // Convert string to float using Float.valueOf()
        float floatValue = Float.valueOf(strValue);

        System.out.println("String value: " + strValue);
        System.out.println("Float value: " + floatValue);
    }
}

Output:

String value: 678.90
Float value: 678.9

Explanation:

  • Float.valueOf(strValue) converts the string value to its float representation.

3. Using DecimalFormat

The DecimalFormat class allows you to parse a string into a float using a specific pattern.

Example:

import java.text.DecimalFormat;
import java.text.ParseException;

public class StringToFloatUsingDecimalFormat {
    public static void main(String[] args) {
        String strValue = "1234.56";

        // Convert string to float using DecimalFormat
        DecimalFormat decimalFormat = new DecimalFormat("#.##");
        try {
            float floatValue = decimalFormat.parse(strValue).floatValue();

            System.out.println("String value: " + strValue);
            System.out.println("Float value: " + floatValue);
        } catch (ParseException e) {
            e.printStackTrace();
        }
    }
}

Output:

String value: 1234.56
Float value: 1234.56

Explanation:

  • decimalFormat.parse(strValue).floatValue() converts the string value to its float representation using the specified pattern.

4. Complete Example Program

Here is a complete program that demonstrates all the methods discussed above to convert a String to a Float.

Example Code:

import java.text.DecimalFormat;
import java.text.ParseException;

public class StringToFloatExample {
    public static void main(String[] args) {
        String strValue1 = "123.45";
        String strValue2 = "678.90";
        String strValue3 = "1234.56";

        // Using Float.parseFloat() Method
        float floatValue1 = Float.parseFloat(strValue1);
        System.out.println("Using Float.parseFloat():");
        System.out.println("String value: " + strValue1 + " -> Float value: " + floatValue1);

        // Using Float.valueOf() Method
        float floatValue2 = Float.valueOf(strValue2);
        System.out.println("\nUsing Float.valueOf():");
        System.out.println("String value: " + strValue2 + " -> Float value: " + floatValue2);

        // Using DecimalFormat
        DecimalFormat decimalFormat = new DecimalFormat("#.##");
        try {
            float floatValue3 = decimalFormat.parse(strValue3).floatValue();
            System.out.println("\nUsing DecimalFormat:");
            System.out.println("String value: " + strValue3 + " -> Float value: " + floatValue3);
        } catch (ParseException e) {
            e.printStackTrace();
        }
    }
}

Output:

Using Float.parseFloat():
String value: 123.45 -> Float value: 123.45

Using Float.valueOf():
String value: 678.90 -> Float value: 678.9

Using DecimalFormat:
String value: 1234.56 -> Float value: 1234.56

5. Conclusion

Converting a String to a Float in Java can be accomplished in several ways. The Float.parseFloat() and Float.valueOf() methods are both straightforward and widely used. Using DecimalFormat provides additional control over the formatting of the float value. By understanding these different methods, you can choose the one that best fits your needs and coding style.

Happy coding!

Comments