JavaDevNotes.com

Java Float To String Without Exponential Scientific Notation

The float type is typically used for storing non whole numbers. It's advantage over double is it consumes less memory, but the drawback is it has less precision. When a float is converted to String, sometimes the result is in scientific notation. Below are examples on how to convert float to String in Java without exponential or scientific notation.

Scenario of Problem

We can easily create a String object from a float value using String.valueOf. For example:
public class Test {
    public static void main(String[] args) {
        float firstNumber = 34f;
        float secondNumber = 0.12f;
        String firstNumberAsString = String.valueOf(firstNumber);
        String secondNumberAsString = String.valueOf(secondNumber);
        System.out.println(firstNumberAsString);
        System.out.println(secondNumberAsString);
    }
}
Will display the result below, which has no problems at all:
34.0
0.12
The example was okay because the number of digits are too few. But a nasty effect is that the String will be in scientific or exponential notation when dealing with numbers that have larger number of digits. Consider the example code below:
public class Test {
    public static void main(String[] args) {
        float firstNumber = 12312312f;
        float secondNumber = 0.00012312312f;
        String firstNumberAsString = String.valueOf(firstNumber);
        String secondNumberAsString = String.valueOf(secondNumber);
        System.out.println(firstNumberAsString);
        System.out.println(secondNumberAsString);
    }
}

The code will have an output with scientific notation, as shown below:

1.2312312E7
1.2312311E-4

Which is not humand friendly and not nice to output to users of our programs.

String.format() Solution

We can use String.format to convert float to String in Java without exponential or scientific notation as shown in the example below:
public class Test {
    public static void main(String[] args) {
        float firstNumber = 12312312f;
        float secondNumber = 0.0001231231f;
        String firstNumberAsString = String.format ("%.0f", firstNumber);
        String secondNumberAsString = String.format("%.10f",secondNumber);
        System.out.println(firstNumberAsString);
        System.out.println(secondNumberAsString);
    }
}
The parameter value of "%.0f" tells the method that we want 0 decimal places, which means we only want the whole number. The parameter value of "%.10f" means we wish 10 decimal places. The code outputs:
12312312
0.0001231231
Beware that float has smaller precision than double. Hence, the output will be weird or there will be lost of details when we use a number with higher precision. For example, let's add the digits 55 at the end of both numbers in our example:
public class Test {
    public static void main(String[] args) { 
        float firstNumber = 1231231255f;
        float secondNumber = 0.000123123155f;
        String firstNumberAsString = String.format ("%.0f", firstNumber);
        String secondNumberAsString = String.format("%.12f",secondNumber);
        System.out.println(firstNumberAsString);
        System.out.println(secondNumberAsString);
    }
}

And the output will be:

1231231232
0.000123123158

Which is weird because 55 was changed to 32 in the first number, and 55 was changed to 58 in the second number. The issue is not with the conversion but with the precision that float can only handle.

DecimalFormat Solution

DecimalFormat can also be used to remove exponential or scientific notation when converting a float to String in Java, as shown in below's example:
import java.text.DecimalFormat;
public class Test {
    public static void main(String[] args) {
        float firstNumber = 12312312f;
        float secondNumber = 0.0001231231f;
        DecimalFormat decimalFormat = new DecimalFormat("0.0000000000");
        String firstNumberAsString = decimalFormat.format(firstNumber);
        String secondNumberAsString = decimalFormat.format(secondNumber);
        System.out.println(firstNumberAsString);
        System.out.println(secondNumberAsString);
    }
}

The output of this code is:

12312312.0000000000
0.0001231231

We can also use DecimalFormat to also put comma for every 3 digits on the whole number (thousand separator).

import java.text.DecimalFormat;
public class Test {
    public static void main(String[] args) {
        float firstNumber = 12312312f;
        DecimalFormat decimalFormat = new DecimalFormat("#,##0.00");
        String firstNumberAsString = decimalFormat.format(firstNumber);
        System.out.println(firstNumberAsString);
    }
}

This will result to:

12,312,312.00