Grails Cookbook - A collection of tutorials and examples

Converting Java Float To String Without Exponential Scientific Notation

The data type float consumes less memory when it comes to storing numbers with decimal points. Although this is the edge of it, the disadvantage is that it's not precise. In one of our posts, we discussed that the result of converting float to String is in scientific notation form. Now we show examples on how to convert float to String in Java without exponential or scientific notation.:

The Issue

One solution to utilize is the String.valueOf from a float value in order to create string object. Check out the codes below:
public class StringValueOfSample {
    public static void main(String[] args) {
        float numberA = 125;
        float numberB = 5.35f;
        String firstNumAsString = String.valueOf(numberA);
        String secondNumAsString = String.valueOf(numberB);
        System.out.println("The converted value is: " + firstNumAsString);
        System.out.println("The converted value is: " + secondNumAsString);
    }
}
The converted value of float into string is acquired and shown below:
The converted value is: 125.0
The converted value is: 5.35
Since the given numbers were uncomplicated, Java converted it into string easily. But if we used larger numbers which has complex digits, we should expect the result with scientific notation and in exponential form. Now take a look at the altered program below:
public class Testing {
    public static void main(String[] args) {
        float numberA = 35923764f;
        float numberB = 0.00035923764f;
        String firstNumAsString = String.valueOf(numberA);
        String secondNumAsString = String.valueOf(numberB);
        System.out.println("The converted value is: " + firstNumAsString);
        System.out.println("The converted value is: " + secondNumAsString);
    }
}
This is the result of the altered version, unlike the previous sample we did, this is in scientific notation. This can be irritating to some developers.
The converted value is: 3.5923764E7
The converted value is: 3.5923763E-4
So how do we solve this? One solution to remove exponential or scientific notation is to use DecimalFormat. Use the written program below as reference.
import java.text.DecimalFormat;
public class SampleDecimalFormat {
    public static void main(String[] args) {
        float numberA = 74629685;
        float numberB = 0.00074629685f;
        DecimalFormat decimalFormat = new DecimalFormat("0.0000000000");
        String firstNumAsString = decimalFormat.format(numberA);
        String secondNumAsString = decimalFormat.format(numberB);
        System.out.println("The converted value is: " + firstNumAsString);
        System.out.println("The converted value is: " + secondNumAsString);
    }
}
The converted value is: 74629688.0000000000
The converted value is: 0.0007462968
Now DecimalFormat can also be used to separate 3 digits through comma as thousand separator. Check out the program below:
import java.text.DecimalFormat;
public class SampleDecimalFormat {
    public static void main(String[] args) {
        float numberA = 74629685f;
        DecimalFormat decimalFormat = new DecimalFormat("#,##0.00");
        String firstNumAsString = decimalFormat.format(numberA);
        System.out.println("The converted value is: " + firstNumAsString);
    }
}
The output is shown below:
The converted value is: 74,629,688.00
Another we can try is String.format to convert float to String in Java without exponential or scientific notation as shown in the example below:
public class Testing {
    public static void main(String[] args) {
        float numberA = 74629685f;
        float numberB = 0.00074629685f;
        String firstNumAsString = String.format ("%.0f", numberA);
        String secondNumAsString = String.format("%.10f",numberB);
        System.out.println("The converted value is: " + firstNumAsString);
        System.out.println("The converted value is: " + secondNumAsString);
    }
}
"%.0f" is for 0 decimal places, meaning - what the method will acquire is the whole number only. And the parameter "%.10f" will obtain numbers with 10 decimal places. The result of the code is shown below:
The converted value is: 74629688
The converted value is: 0.0007462968
Remember that when we work with float, it has smaller precision compared to double. Therefore, the result of the number with higher precision will likely be inaccurate when it comes to details.
public class AnotherApproach {
    public static void main(String[] args) { 
        float numberA = 74629685f;
        float numberB = 0.00074629685f;
        String firstNumAsString = String.format ("%.0f", numberA);
        String secondNumAsString = String.format("%.12f",numberB);
        System.out.println("The converted value is: " + firstNumAsString);
        System.out.println("The converted value is: " + secondNumAsString);
    }
}
Check out the result below:
The converted value is: 74629688
The converted value is: 0.000746296835
Notice that the last two digits. Again, the problem lies not with the conversion but with the precision of the float's capability.