Grails Cookbook - A collection of tutorials and examples

Convert Java Float To String With 2 Decimal Place

Remember we've discussed about double and integer? Now let's go to java Float, and this topic will cover the conversion of java float to String with 2 Decimal Places. But first, we know that Java Float is a data type that's commonly used for representing numerical data with decimal places. Some of us are required to transform the value of float to String for our programs, this post will help you how to operate such process.

Check out the written examples below:


Float To String Using DecimalFormat

There is another option for converting float to string with 2 decimal places and that is the utility class named java.text.DecimalFormat.

Check out the written example on how to apply the DecimalFormat:

/**
 * This is a simple java example that converts a float to String with two decimal places using DecimalFormat.
 */
public class Test {
    public static void main(String[] args) {
        float number = 123.1465f;
        DecimalFormat decimalFormat = new DecimalFormat("#.00");
        String numberAsString = decimalFormat.format(number);
        System.out.println(numberAsString);
    }
}
The purpose of the "#" is to show us the whole number as it is, while the ".00" informs us that two decimal places were utilized.
Don't forget to expect the rounded output and that will get you:
123.15
Now DecimalFormat has the capability to assist also the separation of every three digits with a comma. Check out the written example below:

public class Test { public static void main(String[] args) { float number = 12223.3655f; DecimalFormat decimalFormat = new DecimalFormat("#,##0.00"); String numberAsString = decimalFormat.format(number); System.out.println(numberAsString); } }

12223.37
Since we tackled about this a while ago, we must be cautious of the performance of float precision when we include large values.
One friendly advise would be: use double.

Check out the complexity of the program below:


public class Test { public static void main(String[] args) { DecimalFormat decimalFormat = new DecimalFormat("#,##0.00"); System.out.println(decimalFormat.format(3212355.3655f)); System.out.println(decimalFormat.format(3212355.6585f)); System.out.println(decimalFormat.format(32123551.3655f)); System.out.println(decimalFormat.format(32123551.6585f)); } }

3,212,355.38
3,212,355.62
32,123,551.00
32,123,552.00
As we have witnessed before, the output shows no match for what we have anticipated, and though the rounding values seemed wrong,
the conflict literally lies with the assisted precision of the java float number.

Float To String Using String.format()

From the Java 5 version through the recent ones, the String.format() can be used for this type of conversion.
Observe the written program and understand what we are trying to accomplish, the goal is to convert a float to String with 2 decimal places.
/**
 * This is a simple Java example that converts a float to String using two decimal places through String.format().
 */
public class Test {
    public static void main(String[] args) {
        float number = 888.7893f;
        String numberAsString = String.format ("%.2f", number);
        System.out.println(numberAsString);
    }
}
Understand that the "%.2f" is written specifically because we want to obtain the two decimal places in the variable number
Don't forget to expect that when java float carries more decimal digits, it is automatically rounded.

Observe the expected result shown below:

888.79
If you want to have thousand separator for numerical output, String.format() can do that as well. Check out the modified program with comma separator
for every three numbers, including the conversion of java float to String with 2 decimal places.

Observe the written code below:

/**
 * This is a simple Java example that converts a float to String with two decimal places and using comma 
 * separator through String.format().
 */
public class Test {
    public static void main(String[] args) {
        float number = 12223.3655f;
        String numberAsString = String.format ("%,.2f", number);
        System.out.println(numberAsString);
    }
}

If you noticed before, we used the parameter "%.2f", and in this example, we also did. Only this time, we added the comma ","
therefore making it "%,.2f" rather than "%.2f". The "," we included is for the comma separator on the left side of the number
for every three digits.

The "2" prior to the ".", means two decimal places shall be obtained.

Don't get confused, because is this crucial if you mistype the arrangements of the punctuation marks.

Check out the output below:

12,223.37
There's one warning to consider when working with floats, it can only assists a small-scale precision for the digits.

Observe the demonstration of the result below:

/**
 * This is a simple java example that converts a very large float to String using String.format().
 */
public class Test {
    public static void main(String[] args) {
        System.out.println(String.format ("%,.2f", 3212355.3655f));
        System.out.println(String.format ("%,.2f", 3212355.6585f));
        System.out.println(String.format ("%,.2f", 32123551.3655f));
        System.out.println(String.format ("%,.2f", 32123551.6585f));
    }
}
3,212,355.38
3,212,355.63
32,123,551.00
32,123,552.00
Now logically, the output shown is not the result you were expecting, the reason behind this is because the values we used surpassed the exactness of the java float.