Yes you can do it with String.format:
String result = String.format("%.2f", 10.0 / 3.0);
// result: "3.33"
result = String.format("%.3f", 2.5);
// result: "2.500"
Answer from mostar on Stack OverflowYes you can do it with String.format:
String result = String.format("%.2f", 10.0 / 3.0);
// result: "3.33"
result = String.format("%.3f", 2.5);
// result: "2.500"
You want java.text.DecimalFormat.
DecimalFormat df = new DecimalFormat("0.00##");
String result = df.format(34.4959);
java - Best way to Format a Double value to 2 Decimal places - Stack Overflow
Number formatting with two digits after decimal point in Java - Stack Overflow
java - How to format a number 0..9 to display with 2 digits (it's NOT a date) - Stack Overflow
formatting - How to print a float with 2 decimal places in Java? - Stack Overflow
No, there is no better way.
Actually you have an error in your pattern. What you want is:
DecimalFormat df = new DecimalFormat("#.00");
Note the "00", meaning exactly two decimal places.
If you use "#.##" (# means "optional" digit), it will drop trailing zeroes - ie new DecimalFormat("#.##").format(3.0d); prints just "3", not "3.00".
An alternative is to use String.format:
double[] arr = { 23.59004,
35.7,
3.0,
9
};
for ( double dub : arr ) {
System.out.println( String.format( "%.2f", dub ) );
}
output:
23.59
35.70
3.00
9.00
You could also use System.out.format (same method signature), or create a java.util.Formatter which works in the same way.
Here are couple of ways :
double number = 678.675379823;
System.out.printf("%.2f", number);
If your want to hold the result in a String
String no = String.format("%.2f", number);
System.out.println("Formatted no :"+no);
java.text.DecimalFormat is neat, clean and simple way of formatting numbers upto n number of decimal places.
NumberFormat formatter = new DecimalFormat("##.00");
System.out.println(formatter.format(number));
Though java.text.DecimalFormat is a nice utility class and allow you to dynamically format numbers in Java it has one problem that its not thread-safe or synchronized.So be careful in multi-threaded environment properly.
You can use something like this :
public class DecimalPlaces {
public static void main(String[] args) {
double d = 1.234567;
System.out.printf("%1$.2f", d);
}
}
OR
public void GetTwoDecimal(){
double d = 2.34568;
DecimalFormat f = new DecimalFormat("##.00"); // this will helps you to always keeps in two decimal places
System.out.println(f.format(d));
}
You can use the printf method, like so:
System.out.printf("%.2f", val);
In short, the %.2f syntax tells Java to return your variable (val) with 2 decimal places (.2) in decimal representation of a floating-point number (f) from the start of the format specifier (%).
There are other conversion characters you can use besides f:
d: decimal integero: octal integere: floating-point in scientific notation
You can use DecimalFormat. One way to use it:
DecimalFormat df = new DecimalFormat();
df.setMaximumFractionDigits(2);
System.out.println(df.format(decimalNumber));
Another one is to construct it using the #.## format.
I find all formatting options less readable than calling the formatting methods, but that's a matter of preference.
You can use String.format("%.2f", d) , your double will be rounded automatically
pieChart.setCenterText("$ " + String.format("%.2f", d));
Following code might help you
double a = 1.234567;
double a = 2;
NumberFormat nf = new DecimalFormat("##.##");
System.out.println(nf.format(a));
System.out.println(nf.format(a));
and the output will be
1.23
2
it only show decimal places if needed, Enjoy! :)
One of the way would be using NumberFormat.
NumberFormat formatter = new DecimalFormat("#0.00");
System.out.println(formatter.format(4.0));
Output:
4.00
With Java 8, you can use format method..: -
System.out.format("%.2f", 4.0); // OR
System.out.printf("%.2f", 4.0);
fis used forfloatingpoint value..2after decimal denotes, number of decimal places after.
For most Java versions, you can use DecimalFormat: -
DecimalFormat formatter = new DecimalFormat("#0.00");
double d = 4.0;
System.out.println(formatter.format(d));