I'm having some problems formatting the decimals of a double. If I have a double value, e.g. 4.0, how do I format the decimals so that it's 4.00 instead?
-
1Try using [`String.format()`](http://docs.oracle.com/javase/7/docs/api/java/lang/String.html#format(java.lang.String,%20java.lang.Object...)) or [`java.text.Format`](http://docs.oracle.com/javase/7/docs/api/java/text/Format.html). – Code-Apprentice Oct 09 '12 at 18:39
-
1possible duplicate of [Show only two digit after decimal](http://stackoverflow.com/questions/10959424/show-only-two-digit-after-decimal) – Raedwald Apr 03 '14 at 12:06
-
Possible duplicate of [Best way to Format a Double value to 2 Decimal places](http://stackoverflow.com/questions/8819842/best-way-to-format-a-double-value-to-2-decimal-places) – Keale Oct 21 '15 at 02:11
13 Answers
One of the way would be using NumberFormat.
NumberFormat formatter = new DecimalFormat("#0.00");
System.out.println(formatter.format(4.0));
Output:
4.00

- 65,990
- 13
- 130
- 167
-
1
-
I'm getting a comma. This is what i did. "double variable; NumberFormat formatter = new DecimalFormat("#0.00"); System.out.println(formatter.format(variable)); – Christoffer Oct 09 '12 at 18:53
-
-
10You're getting a comma because of the default language used by your JVM, which is probably different from those of Nambari. Have a look at NumberFormat javadoc : http://docs.oracle.com/javase/1.4.2/docs/api/java/text/NumberFormat.html – Michael Zilbermann Oct 09 '12 at 18:54
-
1As zim2001 pointed, your locale might be different. Read the link provided in my answer. At the end you will find info about this. – kosa Oct 09 '12 at 18:57
-
1Optionally u might want to add another separator for digits more than 3 before the comma then use the following `NumberFormat formatter = new DecimalFormat("#,000.00"); System.out.println(formatter.format(4.0));` – Jose Mhlanga Aug 27 '20 at 07:01
With Java 8, you can use format
method..: -
System.out.format("%.2f", 4.0); // OR
System.out.printf("%.2f", 4.0);
f
is used forfloating
point value..2
after 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));

- 12,476
- 16
- 84
- 127

- 209,639
- 45
- 409
- 525
Use String.format:
String.format("%.2f", 4.52135);
As per docs:
The locale always used is the one returned by
Locale.getDefault()
.

- 142,137
- 41
- 261
- 360

- 28,208
- 16
- 81
- 124
-
8String.format also accepts a locale as a first parameter to account for decimal sign formatting. – Vincent Mimoun-Prat Oct 09 '12 at 18:41
-
4To avoid locale-specific decimal signs (such as a comma), specify the US locale: String.format(Locale.US,"%.2f", 4.52135); – Alexander233 Sep 22 '17 at 20:16
-
4I would say "to use US locale-specific dot as a decimal sign, specify the US locale" .. this idea that US habits are the right ones and everything else is kind of "folk" stuff drives me crazy. – Simone Gianni Mar 03 '18 at 12:05
-
@SimoneGianni Don't make stupid assumptions, some people might need the decimal to be a dot. For example I am writing a function that exports a CSV document, and I can't have a comma because the comma character is the delimiter. – Kaiser Keister May 04 '20 at 00:26
Using String.format, you can do this:
double price = 52000;
String.format("$%,.2f", price);
Notice the comma which makes this different from @Vincent's answer
Output:
$52,000.00
A good resource for formatting is the official java page on the subject

- 26,627
- 26
- 120
- 132
You could always use the static method printf from System.out
- you'd then implement the corresponding formatter; this saves heap space in which other examples required you to do.
Ex:
System.out.format("%.4f %n", 4.0);
System.out.printf("%.2f %n", 4.0);
Saves heap space which is a pretty big bonus, nonetheless I hold the opinion that this example is much more manageable than any other answer, especially since most programmers know the printf function from C (Java changes the function/method slightly though).

- 75
- 7
-
1@Baz Am I missing a crucial detail here, there's no printStream mentioned. The OP is asking for formatting help (which could be solved more easily by looking at the Java docs) to the standard output stream. Explain. – Lewis Robbins Oct 09 '12 at 18:46
-
My bad. Was looking at the wrong javadoc version (1.4). BTW: `out` is the `PrintStream`. – Baz Oct 09 '12 at 18:47
-
@Baz No worries. Yes, I assumed that's the class you were referring to, in all my books printf is referred to as the static method from the System.out (as those methods are static). – Lewis Robbins Oct 09 '12 at 18:47
-
1To be exact, `printf` is a static method of `PrintStream`. See [here](http://docs.oracle.com/javase/7/docs/api/java/io/PrintStream.html) ;) But +1 nonetheless. – Baz Oct 09 '12 at 18:52
-
@Baz From the docs the printStream class inherited System.out. I could perhaps be wrong - although it is quite strange to inherit a static method (is it even possible)? You're, I expect correct though. – Lewis Robbins Oct 09 '12 at 18:58
-
I can't follow you. `System.out` is an static instance of `PrintStream`. That's why you can access methods of `PrintStream` when using `System.out`. – Baz Oct 09 '12 at 18:59
-
double d = 4.0;
DecimalFormat nf = DecimalFormat.getInstance(Locale.ENGLISH);
System.out.println(nf.format("#.##"));

- 1,398
- 9
- 19
You can use any one of the below methods
If you are using
java.text.DecimalFormat
DecimalFormat decimalFormat = NumberFormat.getCurrencyInstance(); decimalFormat.setMinimumFractionDigits(2); System.out.println(decimalFormat.format(4.0));
OR
DecimalFormat decimalFormat = new DecimalFormat("#0.00"); System.out.println(decimalFormat.format(4.0));
If you want to convert it into simple string format
System.out.println(String.format("%.2f", 4.0));
All the above code will print 4.00

- 341
- 3
- 5
An alternative method is use the setMinimumFractionDigits
method from the NumberFormat
class.
Here you basically specify how many numbers you want to appear after the decimal point.
So an input of 4.0
would produce 4.00
, assuming your specified amount was 2.
But, if your Double
input contains more than the amount specified, it will take the minimum amount specified, then add one more digit rounded up/down
For example, 4.15465454
with a minimum amount of 2 specified will produce 4.155
NumberFormat nf = NumberFormat.getInstance();
nf.setMinimumFractionDigits(2);
Double myVal = 4.15465454;
System.out.println(nf.format(myVal));

- 4,176
- 4
- 17
- 40
Works 100%.
import java.text.DecimalFormat;
public class Formatting {
public static void main(String[] args) {
double value = 22.2323242434342;
// or value = Math.round(value*100) / 100.0;
System.out.println("this is before formatting: "+value);
DecimalFormat df = new DecimalFormat("####0.00");
System.out.println("Value: " + df.format(value));
}
}

- 2,214
- 6
- 33
- 47
First import NumberFormat
. Then add this:
NumberFormat currencyFormatter = NumberFormat.getCurrencyInstance();
This will give you two decimal places and put a dollar sign if it's dealing with currency.
import java.text.NumberFormat;
public class Payroll
{
/**
* @param args the command line arguments
*/
public static void main(String[] args)
{
int hoursWorked = 80;
double hourlyPay = 15.52;
double grossPay = hoursWorked * hourlyPay;
NumberFormat currencyFormatter = NumberFormat.getCurrencyInstance();
System.out.println("Your gross pay is " + currencyFormatter.format(grossPay));
}
}
-
1The question is not about displaying a currency and is more accurately answered by using formatting functions )like described in other answers). – Yacc Oct 27 '15 at 23:29
You can do it as follows:
double d = 4.0;
DecimalFormat df = new DecimalFormat("#.##");
System.out.print(df.format(d));
I know that this is an old topic, but If you really like to have the period instead of the comma, just save your result as X,00 into a String and then just simply change it for a period so you get the X.00
The simplest way is just to use replace.
String var = "X,00";
String newVar = var.replace(",",".");
The output will be the X.00 you wanted. Also to make it easy you can do it all at one and save it into a double variable:
Double var = Double.parseDouble(("X,00").replace(",",".");
I know that this reply is not useful right now but maybe someone that checks this forum will be looking for a quick solution like this.