Java
float conversion
string conversion
Java programming
data types

Convert float to String and String to float in Java

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Introduction

Converting between float and String in Java is easy at the syntax level, but the right method depends on whether you care about round-trip accuracy, user-facing formatting, or locale-specific input. The safest habit is to separate machine conversion from display formatting.

Convert float to String

For normal machine-readable conversion, use Float.toString or String.valueOf.

java
1public class Main {
2    public static void main(String[] args) {
3        float value = 123.4567f;
4
5        String a = Float.toString(value);
6        String b = String.valueOf(value);
7
8        System.out.println(a);
9        System.out.println(b);
10    }
11}

These methods are appropriate when the text representation is meant for logs, configuration, APIs, or any other place where the number should remain a number rather than a user-formatted display string.

Convert String to float

To parse a numeric string, use Float.parseFloat.

java
1public class Main {
2    public static void main(String[] args) {
3        String text = "123.4567";
4        float value = Float.parseFloat(text);
5        System.out.println(value);
6    }
7}

If the string is invalid, Java throws NumberFormatException. That is good for strict input validation, but it means external input should be handled carefully.

Safe Parsing Helper

When input may be invalid, a helper method can keep parsing logic centralized.

java
1public class Main {
2    public static Float tryParseFloat(String input) {
3        try {
4            return Float.parseFloat(input);
5        } catch (NumberFormatException ex) {
6            return null;
7        }
8    }
9
10    public static void main(String[] args) {
11        System.out.println(tryParseFloat("3.14"));
12        System.out.println(tryParseFloat("not-a-number"));
13    }
14}

Whether you return null, Optional<Float>, or throw a custom exception depends on the style of the application.

Formatting for Display Is a Different Problem

If the value is meant for users, you often want controlled decimal places. That is formatting, not conversion.

java
1import java.text.DecimalFormat;
2
3public class Main {
4    public static void main(String[] args) {
5        float value = 123.4567f;
6        DecimalFormat df = new DecimalFormat("0.00");
7        String display = df.format(value);
8        System.out.println(display);
9    }
10}

This is good for UI or reports, but it is not always appropriate for internal serialization because formatting may round the value.

Locale Matters for User Input

Float.parseFloat expects a dot as the decimal separator. User input from some locales may use a comma instead. In those cases, parse with a locale-aware formatter.

java
1import java.text.NumberFormat;
2import java.util.Locale;
3
4public class Main {
5    public static void main(String[] args) throws Exception {
6        NumberFormat nf = NumberFormat.getInstance(Locale.GERMANY);
7        Number number = nf.parse("123,45");
8        float value = number.floatValue();
9        System.out.println(value);
10    }
11}

This is a display and input concern, not a machine protocol concern. For APIs and file formats, it is usually better to define one canonical numeric format explicitly.

Scientific Notation Is Accepted

Float.parseFloat also accepts scientific notation.

java
1public class Main {
2    public static void main(String[] args) {
3        System.out.println(Float.parseFloat("1e-3"));
4        System.out.println(Float.parseFloat("2.5E2"));
5    }
6}

If your application should forbid scientific notation, you need validation before parsing or a stricter parser policy.

Use BigDecimal for Exact Decimal Domains

If the value represents money or anything requiring exact decimal semantics, float is usually the wrong type because it uses binary floating-point representation.

java
1import java.math.BigDecimal;
2
3public class Main {
4    public static void main(String[] args) {
5        BigDecimal amount = new BigDecimal("123.45");
6        String text = amount.toPlainString();
7        System.out.println(text);
8    }
9}

This is not just a precision detail. It changes whether the type is suitable for the domain at all.

Testing Round-Trip Behavior

When conversion correctness matters, test the round-trip explicitly.

java
1public class Main {
2    public static void main(String[] args) {
3        float original = 0.1f;
4        String text = Float.toString(original);
5        float parsed = Float.parseFloat(text);
6
7        System.out.println(original == parsed);
8    }
9}

That confirms the machine conversion path behaves as expected for standard float values.

Common Pitfalls

A common mistake is using display-formatted strings in later computations. Formatting often rounds or localizes the value.

Another mistake is ignoring locale when parsing user-entered numbers.

Developers also sometimes use float for exact decimal domains such as currency, where BigDecimal would be the correct choice.

Finally, always expect NumberFormatException when parsing external input. Parsing is a validation boundary, not just a conversion step.

Summary

  • Use Float.toString or String.valueOf to convert a float to text.
  • Use Float.parseFloat to parse standard machine-style numeric strings.
  • Keep display formatting separate from core numeric conversion.
  • Use locale-aware parsing for user input when decimal separators vary.
  • Use BigDecimal instead of float when the domain requires exact decimal values.

Course illustration
Course illustration

All Rights Reserved.