String strDouble = String.format("%.2f", 1.23456);
It will format the floating level quantity 1.23456 up-to 2 decimal locations, as a result of we now have used two after decimal level in formatting instruction %.2f, f is for floating level quantity, which incorporates each double and float knowledge kind in Java. Do not attempt to use “d” for double right here, as a result of that’s used to format integer and stands for decimal in formatting instruction.
String strDouble = String.format(“%.2f”, 1.9999); System.out.println(strDouble); // print 2.00
As I mentioned earlier than, this isn’t the one option to format floating level numbers in Java, you should use proper from the highest Formatter class, which gives format() methodology just like String’s format() methodology. Alternatively, for those who simply need to print formatted floating level numbers into console, you should use System.printf() methodology, which successfully mix above two strains into single one.
However one of the best methodology for activity is utilizing DecmialFormat class, which is definitely designed to format any quantity in Java be it integer, float or double. Whereas creating occasion of DecimalFormat class, you’ll be able to go it a formatting string, which is bit totally different then what you go to those format methodology, however I suppose its extra readable. This string specifies up-to what number of decimal locations you need to format the enter. Here’s a fast instance of formatting double and float numbers utilizing DecimalFormat class.
DecimalFormat df = new DecimalFormat("#.##"); String formatted = df.format(2.456345); System.out.println(formatted); //prints 2.46
The string “#.##” point out that we’re formatting up-to 2 decimal factors, “#.###” signifies formatting quantity up-to 3 decimal locations. By the best way, even DecimalFormat rounds the quantity if subsequent decimal level is greater than 5.
By the best way, there’s refined distinction between formatting floating level numbers utilizing String.format() and DecimalFormat.format(), former will at all times print trailing zeros even when there is no such thing as a fractional half. For instance for those who format 2.00034 up-to two decimal locations String’s format() methodology will print “2.00”, whereas format() methodology of DecimalFormat class will print “2”, as proven beneath :
String strDouble = String.format("%.2f", 2.00023); System.out.println(strDouble); // print 2.00 DecimalFormat df = new DecimalFormat("#.##"); String formatted = df.format(2.00023); System.out.println(formatted); //prints 2
It is a very refined however helpful distinction, its straightforward to overlook however pays properly while you bear in mind it. Now you can determine what to make use of relying upon whether or not you want trailing zeros or not.
Formatting Floating Level Quantity in Java
Right here is our full Java program to format floating level numbers in Java. It consists of all of the methods, we now have mentioned to date to format a float or double variable up-to sure decimal locations in Java. You should use any of those approach to fairly print any float or double variable in Java. I personally like to make use of DecimalFormat for its readability benefit however like SimpleDateFormat, that is additionally an costly object and never thread-safe, so use this with warning.
Yet another factor to contemplate is trailing zeros, if you need trailing zeros e.g. need to print 2 as “2.00″ then use String class’ format methodology, in any other case use DecimalFormat’s format methodology.
import java.util.Arrays; import java.util.Formatter; /** * Java program to format float or double to String in Java. On this Java * instance, you'll discover ways to show a floating level quantity up-to 2 or 3 * decimal locations. * * @writer Javin Paul */ public class FormatFloatInJava { public static void primary(String args[]) { // First activity - format a floating level quantity up-to 2 decimal locations float pi = 3.1428733f; // From Java 5, String has a format() methodology String str = String.format("%.02f", pi); System.out.println("formatted float as much as 2 decimals " + str); // When you simply need to show, you'll be able to mix above two // by utilizing printf() // syntax of formatting will stay similar // this can show floating level quantity up-to 3 decimals System.out.printf("floating level quantity up-to 3 decimals : %.03f %n", pi); // Alternatively you can too use Formatter class to // format floating level numbers // Allocate a Formatter on the StringBuilder StringBuilder sb = new StringBuilder(); Formatter formatter = new Formatter(sb); // Ship all outputs to StringBuilder // format() has the identical syntax as printf() formatter.format("%.4f", pi); // 4 decimal locations System.out.println("Worth of PI as much as 4 decimals : " + formatter.toString()); formatter.shut(); // Equally you'll be able to format double to String in Java double worth = 20.25; System.out.printf("Worth of double up-to 2 decimals : %.2f", worth); // greatest option to format floating level numbers in Java // beware it additionally around the numbers DecimalFormat df = new DecimalFormat("#.##"); String formatted = df.format(2.456345); System.out.println(formatted); //prints 2.46 } } Output: formatted float up-to 2 decimals 3.14 floating level quantity up-to three decimals : 3.143 Worth of PI as much as 4 decimals : 3.1429 Worth of double up-to 2 decimals : 20.25 2.46
I simply bear in mind there’s one other option to fairly print floating-point quantity in Java, by utilizing the setMaximumFractionDigits(int locations) methodology from NumberFormat class. you’ll be able to simply go the variety of digits you need to maintain decimals, for instance, to format a quantity as much as 4 decimal locations, go 4. Here’s a fast instance of this :
double easy = 4.0099; double spherical = 4.9999; NumberFormat nf = NumberFormat.getInstance(); nf.setMaximumFractionDigits(3); System.out.println(nf.format(easy)); // prints 4.01 System.out.println(nf.format(spherical)); // prints 5
That is all about how you can format floating-point numbers in Java. Now we have seen the next 4 methods to format a float or double quantity as String in Java :
- Through the use of the String format() methodology
- Through the use of the DecimalFormat format() methodology
- Through the use of printf() methodology
- Through the use of Formatter’s format() methodology
- Through the use of setMaximumFractionDigits() of NumberFormat class
You should use any of those approaches to format floating-point numbers, simply keep in mind that String’s format() will at all times print trailing zeros even when there is no such thing as a extra fractional half, whereas DecimalFormat, NumberFormat courses is not going to maintain that.