aveschini
aveschini

Reputation: 1712

Java: double: how to ALWAYS show two decimal digits

I use double values in my project and I would like to always show the first two decimal digits, even if them are zeros. I use this function for rounding and if the value I print is 3.47233322 it (correctly) prints 3.47. But when I print, for example, the value 2 it prints 2.0.

public static double round(double d) {
    BigDecimal bd = new BigDecimal(d);
    bd = bd.setScale(2, BigDecimal.ROUND_HALF_UP);
    return bd.doubleValue();
}

I want to print 2.00!

Is there a way to do this without using Strings?

EDIT: from your answers (which I thank you for) I understand that I wasn't clear in telling what I am searching (and I'm sorry for this): I know how to print two digits after the number using the solutions you proposed... what i want is to store in the double value directly the two digits! So that when I do something like this System.out.println("" + d) (where d is my double with value 2) it prints 2.00.

I'm starting to think that there is no way to do this... right? Thank you again anyway for your answers, please let me know if you know a solution!

Upvotes: 29

Views: 95382

Answers (7)

I would just use something like:

System.out.printf("%.2f", theValueYouWantToPrint); 

This gives you two decimals.

Upvotes: 1

Sagar Vora
Sagar Vora

Reputation: 11

You can use something like this:

If you want to retain 0 also in the answer: then use (0.00) in the format String

double d = 2.46327;
DecimalFormat df = new DecimalFormat("0.00");
System.out.print(df.format(d));

The output: 2.46

double d = 0.0001;
DecimalFormat df = new DecimalFormat("0.00");
System.out.print(df.format(d));

The output: 0.00

However, if you use DecimalFormat df = new DecimalFormat("0.##");

double d = 2.46327;
DecimalFormat df = new DecimalFormat("0.##");
System.out.print(df.format(d));

The output: 2.46

double d = 0.0001;
DecimalFormat df = new DecimalFormat("0.##");
System.out.print(df.format(d));

The output: 0

Upvotes: 1

Vikash Kumar
Vikash Kumar

Reputation: 11

java.text.DecimalFormat df = new java.text.DecimalFormat("###,###.##");
df.setMaximumFractionDigits(2); 
df.setMinimumFractionDigits(2); 

Upvotes: 1

darijan
darijan

Reputation: 9795

Use the java.text.NumberFormat for this:

NumberFormat nf= NumberFormat.getInstance();
nf.setMaximumFractionDigits(2);
nf.setMinimumFractionDigits(2);
nf.setRoundingMode(RoundingMode.HALF_UP);

System.out.print(nf.format(decimalNumber));

Upvotes: 19

Miral Sarwar
Miral Sarwar

Reputation: 327

You can simply do this:

double d = yourDoubleValue;  
String formattedData = String.format("%.02f", d);

Upvotes: 8

Peter Jaloveczki
Peter Jaloveczki

Reputation: 2089

You can use something like this:

 double d = 1.234567;
 DecimalFormat df = new DecimalFormat("#.00");
 System.out.print(df.format(d));

Edited to actually answer the question because I needed the real answer and this came up on google and someone marked it as the answer despite the fact that this wasn't going to work when the decimals were 0.

Upvotes: 56

Mathias G.
Mathias G.

Reputation: 5105

DecimalFormat is the easiest option to use:

double roundTwoDecimals(double d) {
        DecimalFormat twoDecimals = new DecimalFormat("#.##");
        return Double.valueOf(twoDecimals.format(d));
}

Hope this solves your issue...

Upvotes: 4

Related Questions