105

7
+1  Q:

## How to save decimal in java

Having the following code in Java:

double operation = 890 / 1440;
System.out.println(operation);

Result: 0.0

What I want is to save the first 4 decimal digits of this operation (0.6180). Do you know how can I do it?

+10  A:

Initialize your variable with an expression that evaluates to a double rather than an int:

``````double operation = 890.0 / 1440.0;
``````

Otherwise the expression is done using integer arithmetic (which ends up truncating the result). That truncated result then gets converted to a `double`.

thanks for your help Michael! :)
Although this is pretty, it may be worthwhile to instead (or as well) explicitly cast both numbers to doubles. The reason is that if someone (even the OP later on) comes in later and changes the numbers, they may forget to add the decimals and the functionality will mysteriously break.
I agree, Bozho's 123d solution is preferred as the intent clearer.
+3  A:

You can use the double literal `d` - otherwise your numbers are considered of type `int`:

``````double operation = 890d / 1440d;
``````

Then you can use a `NumberFormat` to specify the number of digits.

For example:

``````NumberFormat format = new DecimalFormat("#.####");
System.out.println(format.format(operation));
``````
@Bozho Does NumberFormat allow me to save four decimal digits in a variable or it only displays the first "X" decimal digits?
NumberFormat only affects the presentation, the variable 'operation' is not affected
@Kennet ok, thanks :)
A:

This is done using BigDecimal

``````   import java.math.BigDecimal;
import java.math.RoundingMode;

public class DecimalTest {

/**
* @param args
*/
public static void main(String[] args) {
double operation = 890.0 / 1440.0;
BigDecimal big = new BigDecimal(operation);
big = big.setScale(4, RoundingMode.HALF_UP);
double d2 = big.doubleValue();
System.out.println(String.format("operation : %s", operation));
System.out.println(String.format("scaled : %s", d2));
}
}
``````

Output

operation : 0.6180555555555556 scaled : 0.6181

+1  A:

You can also do something like this:

``````double result = (double) 890 / 1400;
``````

which prints the following:

0.6180555555555556

You can check how to round up the number here

Just remember, you're not casting (890/1440) to double with this. You're casting 890 to double with this. And a (double/int) operation returns a double.
Yeah. But I think that this method is a bit better since it also works when you have double variables instead of hard coded ones, at least, in C# I used to experience the same problem and fixed it as above.
A:

BigDecimal, although very clumsy to work with, gives some formatting options:

``````    BigDecimal first = new BigDecimal(890);
BigDecimal second = new BigDecimal(1440);
System.out.println(first.divide(second, new MathContext(4, RoundingMode.HALF_EVEN)));
``````
A:

If you really want to round to the first 4 fractional digits you can also use integer arithmetic by first multiplying the first number so its digits are shifted the right amount f places to the left:

``````long fractionalPart = 10000L * 890L / 1440L;
``````

I'm using long here to avoid any overflows in case the temporary result does not fit in 32 bits.

A:
``````double operation = 890.0 / 1440;
System.out.printf(".4f\n", operation);
``````