Check out the written examples below:
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.
123.15Now 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.37Since we tackled about this a while ago, we must be cautious of the performance of float precision when we include large values.
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.00As we have witnessed before, the output shows no match for what we have anticipated, and though the rounding values seemed wrong,
/** * 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
Observe the expected result shown below:
888.79If you want to have thousand separator for numerical output, String.format() can do that as well. Check out the modified program with comma separator
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.37There'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.00Now 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.