16

When I divide 317 by 219 in Java using doubles I get 1.
For example:

double b = 317/219;
System.out.println(b);

Output is: 1.

Is this because it is a recurring decimal? Have had to use BigDecimal instead which is annoying.

Linus Kleen
  • 33,871
  • 11
  • 91
  • 99
Joe
  • 165
  • 1
  • 1
  • 5
  • output will be 1.0 to be more precise – Ajay Gupta Sep 07 '15 at 17:02
  • Possible duplicate of [Java Integer division: How do you produce a double?](http://stackoverflow.com/questions/3144610/java-integer-division-how-do-you-produce-a-double). I took my choice, but this question is asked way too many times on SO. – demongolem Oct 07 '15 at 21:12

7 Answers7

29

Try this

 double b = 317/219D;

The default type of coded numbers in java is int, so with the code as you have it java is working with two int numbers and the result of the division would then be int too, which will truncate the decimal part to give a final result of 1. This int result is then cast from int 1 to a double 1 without a compiler warning because it's a widening cast (one where the source type is guaranteed to "fit" into the target type).

By coding either of the numbers as double with the trailing D (you may also use d, but I always use upper case letters because L as lowercase l looks like a 1), the result of the division will be double too.

Bohemian
  • 412,405
  • 93
  • 575
  • 722
  • 1
    Most other answers just stick `.0` to force floating point (double) arithmetic, but this answer explicitly states both operands to be of type double; a subtle but significant point! – Anders R. Bystrup Dec 02 '12 at 09:43
  • I was sure I'd tried casting to double! Guess I was just being stupid. Thanks for the help! – Joe Dec 02 '12 at 09:54
  • 1
    @Joe You need to explicitly cast one of the inputs to the division -- the implicit cast done by the assignment is equivalent to `(double) (317 / 219)` which, when it's written like that, clearly doesn't do what you want. If you do `(double) a / b`, that should work (casts have higher precedence than division, so it's interpreted as `((double) a) / b`, which isn't the case in all languages) – Nic Nov 26 '18 at 22:37
7

Another alternative...

double b = (double)317/219;
xagyg
  • 9,562
  • 2
  • 32
  • 29
3

This is because you have used integer literals, so you're doing an integer division.

Try writing double b = 317.0/219.0; instead.

Dawood ibn Kareem
  • 77,785
  • 15
  • 98
  • 110
2

It is worth mentioning that there is no division in your example at runtime. 317/219 is calculated at compile-time (integer division, fraction is discarded) and replaced with a constant. If you decompile .class (I used Jad http://www.kpdus.com/jad.html) you will see

double b = 1.0D;
Evgeniy Dorofeev
  • 133,369
  • 30
  • 199
  • 275
0

This is int dividing. Write:

double b = 317.0/219;
System.out.println(b);
bellum
  • 3,642
  • 1
  • 17
  • 22
0

since numbers you put are inetgers so is the answer.
to get double you need either to use a number with floating point or to cast one of the integers you use:

double b = 317.0/219;
System.out.println(b);

or:

double b = ((double)317)/219;
System.out.println(b);
Ofir Luzon
  • 10,635
  • 3
  • 41
  • 52
-3

You could try

double b = 0.0;
b = 317/219;

so that i can return the decimal point

  • 1
    This is not the right answer. both 317 and 219 are int therefore the result will be the int part of the division not matter what is the parameter type. – zohar Oct 01 '15 at 03:41
  • There can be many things you can try other than above. However @Joe had been having trouble understanding how arithmatic stuff works in Java. This shouldn't be an answer at all. – retromuz Oct 07 '16 at 00:45