Can I do it with System.out.print?
18 Answers
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 integer
- o: octal integer
- e: floating-point in scientific notation
 
    
    - 929
- 1
- 14
- 36
 
    
    - 90,123
- 14
- 117
- 115
- 
                    29Please be carefull as String.format depend on your current Local configuration, you may not get a dot as a separator. Prefer using `String.format(java.util.Locale.US,"%.2f", val);` – Gomino Mar 02 '16 at 16:59
- 
                    3@gomino Why not `Locale.FRANCE`? – erickson May 27 '16 at 20:17
- 
                    @erickson in France the decimal separator is a comma and not a dot. – Gomino May 27 '16 at 20:22
- 
                    4@gomino Exactly. So why would you put dots in numbers for French users? – erickson May 27 '16 at 20:27
- 
                    1I added this comment as a reminder to warn people who expect to always have a dot as a separator. – Gomino May 27 '16 at 20:29
- 
                    10@gomino That makes sense, but I think to "prefer" hard-coding `Locale.US` goes too far. If you need to hard-code a "neutral" locale for case-folding, number rendering, etc., specify `Locale.ROOT`. This would be appropriate for text that will be consumed by another program, rather than rendered for human users. For text presented to a user, honor their locale, whether they specified it explicitly or it's the default. – erickson May 27 '16 at 20:37
- 
                    how can I store the result `"%.2f",floatNumber` in a `variable` – Azhar Uddin Sheikh Apr 02 '22 at 05:31
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.
- 
                    12What happened with the `System.out.printf("%.2f", value)` syntax? Is it still around? – Anthony Forloney Mar 29 '10 at 14:49
- 
                    7
- 
                    Looks like it's my option, as I don't know how to use DecimalFormat yet :) Thanks! – via_point Mar 29 '10 at 14:55
- 
                    I haven't done extensive Java work in a while, and when I kept seeing `DecimalFormat` answers I immediately had thought I was wrong, but thank you for clarifying that. – Anthony Forloney Mar 29 '10 at 14:56
- 
                    how can i return float value from this value like :float roundofDecimal(float dd){ DecimalFormat df = new DecimalFormat(); df.setMaximumFractionDigits(2); System.out.println(df.format(dd)); return df.format(dd); } – CoronaPintu Jul 01 '13 at 06:26
- 
                    This is a more useful answer to me than stuff around System.out since I'm not dumping my output to the terminal, but rather in to a GUI. Thank you +1 vote from me. – James T Snell Oct 23 '13 at 01:17
- 
                    12
I would suggest using String.format() if you need the value as a String in your code.  
For example, you can use String.format() in the following way:
float myFloat = 2.001f;
String formattedString = String.format("%.02f", myFloat);
 
    
    - 4,358
- 4
- 35
- 47
double d = 1.234567;
DecimalFormat df = new DecimalFormat("#.##");
System.out.print(df.format(d));
 
    
    - 37,288
- 33
- 152
- 232
- 
                    5Hi Kevin, if if enter 10.0000, i am getting 10 only. If i want to display 10.00 then how can i do? – Mdhar9e Aug 26 '14 at 12:26
- 
                    3
float f = 102.236569f; 
DecimalFormat decimalFormat = new DecimalFormat("#.##");
float twoDigitsF = Float.valueOf(decimalFormat.format(f)); // output is 102.24
 
    
    - 57,942
- 23
- 262
- 279
- 
                    4Depending on the locale you might get a comma. If you want a dot separator, use this: DecimalFormat("#.##", DecimalFormatSymbols(Locale.US)) – DPM Sep 15 '17 at 10:17
You may use this quick codes below that changed itself at the end. Add how many zeros as refers to after the point
float y1 = 0.123456789;
DecimalFormat df = new DecimalFormat("#.00");  
y1 = Float.valueOf(df.format(y1));
The variable y1 was equals to 0.123456789 before. After the code it turns into 0.12 only.
- 
                    2It doesn't display 0 before the point. It should be DecimalFormat df = new DecimalFormat("0.00"); – Ridhuvarshan Feb 20 '20 at 23:31
- 
                    
float floatValue=22.34555f;
System.out.print(String.format("%.2f", floatValue));
Output is 22.35. If you need 3 decimal points change it to "%.3f".
 
    
    - 3,644
- 1
- 27
- 37
 
    
    - 349
- 3
- 7
A simple trick is to generate a shorter version of your variable  by multiplying it with e.g. 100, rounding it and dividing it by 100.0 again. This way you generate a variable, with 2  decimal places:
double new_variable = Math.round(old_variable*100) / 100.0;
This "cheap trick" was always good enough for me, and works in any language (I am not a Java person, just learning it).
 
    
    - 2,012
- 7
- 31
- 63
 
    
    - 109
- 1
- 7
- 
                    2This is wrong, many numbers will be repeating numbers in float representation after you divide by 100. Hence not two decimal places. – matt Mar 29 '16 at 08:54
Many people have mentioned DecimalFormat.  But you can also use printf if you have a recent version of Java:
System.out.printf("%1.2f", 3.14159D);
See the docs on the Formatter for more information about the printf format string.
 
    
    - 588,226
- 146
- 1,060
- 1,140
 
    
    - 13,822
- 6
- 44
- 64
Look at DecimalFormat
Here is an example from the tutorial:
  DecimalFormat myFormatter = new DecimalFormat(pattern);
  String output = myFormatter.format(value);
  System.out.println(value + "  " + pattern + "  " + output);
If you choose a pattern like "###.##", you will get two decimal places, and I think that the values are rounded up. You will want to look at the link to get the exact format you want (e.g., whether you want trailing zeros)
 
    
    - 88,451
- 51
- 221
- 321
To print a float up to 2 decimal places in Java:
    float f = (float)11/3;
    System.out.print(String.format("%.2f",f));
OUTPUT: 3.67
> use %.3f for up to three decimal places.
 
    
    - 201
- 4
- 4
Below is code how you can display an output of float data with 2 decimal places in Java:
float ratingValue = 52.98929821f; 
DecimalFormat decimalFormat = new DecimalFormat("#.##");
float twoDigitsFR = Float.valueOf(decimalFormat.format(ratingValue)); // output is 52.98
 
    
    - 6,073
- 4
- 29
- 47
 
    
    - 10,807
- 1
- 75
- 53
OK - str to float.
package test;
import java.text.DecimalFormat;
public class TestPtz {
  public static void main(String[] args) {
    String preset0 = "0.09,0.20,0.09,0.07";
    String[] thisto = preset0.split(",");    
    float a = (Float.valueOf(thisto[0])).floatValue();
    System.out.println("[Original]: " + a);   
    a = (float) (a + 0.01);
    // Part 1 - for display / debug
    System.out.printf("[Local]: %.2f \n", a);
    // Part 2 - when value requires to be send as it is
    DecimalFormat df = new DecimalFormat();
    df.setMinimumFractionDigits(2);
    df.setMaximumFractionDigits(2);
    System.out.println("[Remote]: " + df.format(a));
  }
}
Output:
run:
[Original]: 0.09
[Local]: 0.10 
[Remote]: 0.10
BUILD SUCCESSFUL (total time: 0 seconds)
One issue that had me for an hour or more, on DecimalFormat- It handles double and float inputs differently. Even change of RoundingMode did not help. I am no expert but thought it may help someone like me. Ended up using Math.round instead. 
See below:
    DecimalFormat df = new DecimalFormat("#.##");
    double d = 0.7750;
    System.out.println(" Double 0.7750 -> " +Double.valueOf(df.format(d)));
    float f = 0.7750f;
    System.out.println(" Float 0.7750f -> "+Float.valueOf(df.format(f)));
    // change the RoundingMode
    df.setRoundingMode(RoundingMode.HALF_UP);
    System.out.println(" Rounding Up Double 0.7750 -> " +Double.valueOf(df.format(d)));
    System.out.println(" Rounding Up Float 0.7750f -> " +Float.valueOf(df.format(f)));
Output:
Double 0.7750 -> 0.78
Float 0.7750f -> 0.77
Rounding Up Double 0.7750 -> 0.78
Rounding Up Float 0.7750f -> 0.77
 
    
    - 49
- 6
public String getDecimalNumber(String number) {
        Double d=Double.parseDouble(number);
        return String.format("%.5f", d);
}
Take care of NumberFormatException as well
 
    
    - 661
- 7
- 8
small simple program for demonstration:
import java.io.*;
import java.util.Scanner;
public class twovalues {
    public static void main(String args[]) {
        float a,b;
        Scanner sc=new Scanner(System.in);
        System.out.println("Enter Values For Calculation");
        a=sc.nextFloat();
        b=sc.nextFloat();
        float c=a/b;
        System.out.printf("%.2f",c);
    }
}
 
    
    - 5,772
- 5
- 41
- 62
 
    
    - 11
- 2
Just do String str = System.out.printf("%.2f", val).replace(",", "."); if you want to ensure that independently of the Locale of the user, you will always get / display a "." as decimal separator. This is a must if you don't want to make your program crash if you later do some kind of conversion like float f = Float.parseFloat(str);
 
    
    - 261
- 4
- 12
Try this:-
private static String getDecimalFormat(double value) {
    String getValue = String.valueOf(value).split("[.]")[1];
      if (getValue.length() == 1) {
          return String.valueOf(value).split("[.]")[0] +
                "."+ getValue.substring(0, 1) + 
                String.format("%0"+1+"d", 0);
       } else {
          return String.valueOf(value).split("[.]")[0]
            +"." + getValue.substring(0, 2);
      }
 }
 
    
    - 111
- 1
- 1
- 7
- 
                    For more Details:- https://makecodesimpleandeasy.blogspot.com/2019/08/get-two-digit-after-decimal-point-in.html – Nathani Software Aug 17 '19 at 10:44
 
     
     
     
    