0

I have a silly problem.

Let's say I have certain double number:

double doubleValue=4.1;

Is there a way to present this value as 4.10 but not as String but rather as double?

user3133542
  • 1,695
  • 4
  • 21
  • 42

4 Answers4

3

If you want to print two decimals do this:

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

this will print 4.10 and the number is a double

Here is a fully working example which you can compile and run to print 4.10:

import java.text.DecimalFormat;

class twoDecimals {
    public static void main(String[] args) {
double d = 4.10;
 DecimalFormat df = new DecimalFormat("#.00");
 System.out.print(df.format(d));
}
}

even if you set

double d = 4.1;

it will print 4.10 .

If you set double d = 4; it will print 4.00 which means always print two decimal points

adrCoder
  • 3,145
  • 4
  • 31
  • 56
  • How would it print 4.10? if it sets **.00**??\ – Apurva Mar 08 '15 at 17:05
  • @Apurva That is the format syntax indicating how many digits of precision you want after the decimal point. In this case, it's two. – Shashank Mar 08 '15 at 17:07
  • Can't guarantee that particular output, as it is locale dependent. e.g. switch to French: `Locale.setDefault(Locale.FRENCH);` before and you will see `4,10`. – weston Mar 08 '15 at 17:26
3

Simply do this,

double doubleValue=4.1;

String.format(Locale.ROOT, "%.2f", doubleValue );

Output:

4.10

Using this approach you don't need to make use of DecimalFormat which will also reduce unnecessary imports

Apurva
  • 7,871
  • 7
  • 40
  • 59
0

You can't define the precision of the data type double. If you need to define the precision of a decimal number you can use the class BigDecimal. As javadoc explains BigDecimal is used for an arbitrary-precision signed decimal numbers. Here is the reference

http://docs.oracle.com/javase/7/docs/api/java/math/BigDecimal.html

Davide Lorenzo MARINO
  • 26,420
  • 4
  • 39
  • 56
0

I don't get why you want double number with 2 digits after decimals with 0 filling the gap. Are you planning to display it somewhere ?

The Answer is , No, you can't.

Actually, you can set the precision of a double if it doesn't have any 0 after that.

For ex -

4.10000 // and you want to set the precision to 2 digits
// it will still give you 4.1 not 4.10

if it is 4.1 // and you want to set precision to 3 digits
// it will still give you 4.1 not 4.100

if it is 4.1234565 // and you want to set precision to 3 digits,
// it will give you 4.123

Even when you format it using String.format and change it back to decimal or use BigDecimal's setScale method to set precision, you can't get a double value ending with 0 after decimal.

If you want to display with certain decimal somewhere, you can do that by converting into String.

Here are 2 methods to do that, (will set precision but will not set 0 at the end to fill the gap)

1.

int numberOfDigitsAfterDecimal = 2;
double yourDoubleResult = 4.1000000000;
Double resultToBeShown = new BigDecimal(yourDoubleResult).setScale(numberOfDigitsAfterDecimal, BigDecimal.ROUND_HALF_UP).doubleValue();
System.out.println(resultToBeShown);

2.

double doubleValue=4.1;
double newValue = Double.parseDouble(String.format( "%.2f", doubleValue ));
System.out.println(newValue);

For getting 4.10 as string ,

double doubleValue=4.1;
String newValue = String.format( "%.2f", doubleValue );
System.out.println(newValue);
Nielarshi
  • 1,126
  • 7
  • 11