Double decimal formatting in Java

I'm having trouble formatting decimal places of two. If I have a double value, for example. 4.0, how do I format decimal places so that it is 4.00 instead?

+103
java double decimal formatting
Oct 09 '12 at 18:36
source share
14 answers

One way to use NumberFormat .

NumberFormat formatter = new DecimalFormat("#0.00"); System.out.println(formatter.format(4.0)); 

Output:

4.00

+201
09 Oct '12 at 18:38
source share

With Java 8, you can use the format .. method: -

 System.out.format("%.2f", 4.0); // OR System.out.printf("%.2f", 4.0); 
  • f used for the value floating .
  • 2 after the decimal place, the number of decimal places after .

For most versions of Java, you can use DecimalFormat : -

  DecimalFormat formatter = new DecimalFormat("#0.00"); double d = 4.0; System.out.println(formatter.format(d)); 
+65
09 Oct '12 at 18:38
source share

Use String.format :

String.format("%.2f", 4.52135);

+39
09 Oct '12 at 18:37
source share

Using String.format, you can do this:

 double price = 52000; String.format("$%,.2f", price); 

Notice the comma that distinguishes it from @Vincent's answer

Output:

$52,000.00

A good resource for formatting is the official java page on the topic.

+17
Mar 18 '16 at 22:09
source share

You can always use the static method printf from System.out - you must implement the appropriate formatter; this saves a ton of space in which you need other examples.

Example:

 System.out.format("%.4f %n", 4.0); System.out.printf("%.2f %n", 4.0); 

It saves a ton of space, which is a pretty big bonus, however, I am of 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 a bit).

+6
Oct 09
source share
 new DecimalFormat("#0.00").format(4.0d); 
+3
09 Oct '12 at 18:39
source share
 double d = 4.0; DecimalFormat nf = DecimalFormat.getInstance(Locale.ENGLISH); System.out.println(nf.format("#.##")); 
+3
09 Oct '12 at 19:00
source share

An alternative method is the setMinimumFractionDigits method of the setMinimumFractionDigits class.

Here you basically specify how many numbers you want to display after the decimal point.

So entering 4.0 will produce 4.00 if your indicated quantity is 2.

But if your Double tab contains more than the specified amount, it takes the specified minimum amount, then adds another digit rounded up / down

For example, 4.15465454 with a minimum of 2 will produce 4.155

 NumberFormat nf = NumberFormat.getInstance(); nf.setMinimumFractionDigits(2); Double myVal = 4.15465454; System.out.println(nf.format(myVal)); 

Try it online

+1
Nov 01 '17 at 8:42 on
source share

It 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)); } } 
0
Jun 03 '16 at 12:30
source share

There are many ways to do this. Those are given below:

Suppose your original number is listed below:

  double number = 2354548.235; 

Using NumberFormat:

 NumberFormat formatter = new DecimalFormat("#0.00"); System.out.println(formatter.format(number)); 

Using String.format:

 System.out.println(String.format("%,.2f", number)); 

Using DecimalFormat and pattern:

 NumberFormat nf = DecimalFormat.getInstance(Locale.ENGLISH); DecimalFormat decimalFormatter = (DecimalFormat) nf; decimalFormatter.applyPattern("#,###,###.##"); String fString = decimalFormatter.format(number); System.out.println(fString); 

Using DecimalFormat and Template

 DecimalFormat decimalFormat = new DecimalFormat("############.##"); BigDecimal formattedOutput = new BigDecimal(decimalFormat.format(number)); System.out.println(formattedOutput); 

In all cases, the output will be: 2354548.23

Note :

During rounding, you can add RoundingMode to your formatter. Here are a few rounding options below:

  decimalFormat.setRoundingMode(RoundingMode.CEILING); decimalFormat.setRoundingMode(RoundingMode.FLOOR); decimalFormat.setRoundingMode(RoundingMode.HALF_DOWN); decimalFormat.setRoundingMode(RoundingMode.HALF_UP); decimalFormat.setRoundingMode(RoundingMode.UP); 

Here is the import :

 import java.math.BigDecimal; import java.math.RoundingMode; import java.text.DecimalFormat; import java.text.NumberFormat; import java.util.Locale; 
0
Apr 07 '19 at 4:50
source share

You can use any of the following methods

  1. 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)); 
  2. If you want to convert it to a simple string format

     System.out.println(String.format("%.2f", 4.0)); 

All code above will print 4.00

0
Aug 15 '19 at 14:44
source share

The first import of NumberFormat . Then add the following:

 NumberFormat currencyFormatter = NumberFormat.getCurrencyInstance(); 

This will give you two decimal places and mark the dollar sign if it deals 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)); } } 
-2
Oct 27 '15 at 22:59
source share

You can do it as follows:

 double d = 4.0; DecimalFormat df = new DecimalFormat("#.##"); System.out.print(df.format(d)); 
-3
09 Oct '12 at 18:39
source share

I know this is an old topic, but if you really like this period instead of a comma, just save your result as X, 00 in String, and then just just change it to a period to get x.00

The easiest way is to simply replace it.

 String var = "X,00"; String newVar = var.replace(",","."); 

The conclusion will be X.00 , which you need. Also, to make it easy, you can do it all on one and save it in a double variable:

 Double var = Double.parseDouble(("X,00").replace(",","."); 

I know this answer is not useful right now, but maybe someone who is checking this forum will be looking for a quick solution like this.

-four
Dec 02 '15 at 19:43
source share



All Articles