49

Sorry for the basic question, I have to divide the long variable by another long variable, but it returns 0. Can any one help

long a = 3004230;
long b =  6793368;
long c = (a/b)*100;
pavel
  • 26,538
  • 10
  • 45
  • 61
Arulmurugan
  • 509
  • 1
  • 5
  • 6
  • 3
    `long c = (long)((a/(float)b)*100);` – Subhrajyoti Majumder Jul 09 '14 at 10:15
  • 3
    did you search your problem before asking question? http://stackoverflow.com/questions/18117014/dividing-long-by-long-returns-0 or http://stackoverflow.com/questions/6766120/long-division-in-java-not-working-as-expected or lots of old questions – Blue Ocean Jul 09 '14 at 10:15
  • 1
    Use `long c = (a*100L/b)` if you don't require decimal value, `long c = (a/b)*100` is evaluated as `(a/b)*100 --> (3004230/6793368)*100 --> (0L)*100 --> c=0` hence it will always be zero whereas `(a*100L/b) --> (300423000/6793368) --> (44L) --> c=44` – Shishir Gupta May 15 '16 at 11:29
  • 1
    long c = 100L *a/b – PeMa Apr 24 '18 at 10:49

7 Answers7

54

Literal Values and Literal Arithmatic

There are a couple of issues with that code. Firstly, non-floating point literal values are of type int by default and so 3004230 in your code is an int. To explicitly declare it a long use 3004230L instead.

Also, all arithmetic done with non-floating point literals returns an int result unless one of the variables are casted specifically to a floating point type such as float or double. As such (a/b)*100 is less than 1, and therefore is truncated down to 0 (the floating point values are just cut off). Also, even if it did return the same result you are trying to store it in a long which can not store floating point values.

So, you should do something like the following to get the real result.

long a = 3004230L; // Use the L notation to declare this literal a long.
long b = 6793368L;
double c = ((double) a/b)*100; /* casting one of the variables to (double) means the result will not be 0 */

I hope this helps.

Rudi Kershaw
  • 12,332
  • 7
  • 52
  • 77
  • 4
    The decimal part of the number is truncated to 0, not rounded down. Cast 0.4 to an `int` and it will be 0, cast 0.99999999999 to an `int` and it will also be 0. The decimal part is removed leaving only the integer part. – Dan Temple Jul 09 '14 at 10:20
  • 1
    @DanTemple - Good point, will correct my answer. – Rudi Kershaw Jul 09 '14 at 10:21
  • There is still a false statement in the comment: "casting your division result to (double) means the result will not be 0". If you try to cast the result it is too late because "((double)(a/b))*100 == 0.0". Still the code is correct because you are actually casting "a" to double and it leads to expected result :) – Marek Manduch Jul 13 '20 at 12:40
  • @MarekManduch quite right. I have corrected it. – Rudi Kershaw Jul 14 '20 at 12:39
  • You don't need the L part in 3004230L as long as the literal value is within size of an int it will auto-convert it to a long for you from int :) – rogerdpack Jun 29 '21 at 18:05
10
final long a = 3004230;
final long b = 6793368;
final double c = ((double) a / b) * 100;

=> c = 44.22298335670907

Cengiz
  • 5,375
  • 6
  • 52
  • 77
6

try it.

long a = 3004230;
long b =  6793368;
long c = ((long)((float)a/(float)b)*100); //answer=44
float c = ((long)((float)a/(float)b)*100); //answer=44.1256378
user3744406
  • 45
  • 1
  • 13
whck6
  • 59
  • 6
4

obviously the answer will be 0 for above..as you can see when you divide

3004230 / 6793368 = 0.442 = 0(when casted to long type)

and

0 * any number = 0..

to convert it use this

double c = (a * 1.0/b)*100.0;

you have to use datatype that can store decimal value which is float or double..long cannot store decimal numbers

rock321987
  • 10,942
  • 1
  • 30
  • 43
4

You can do the same without casting to a float:

long a =  3004230L;
long b =  6793368L;
long c = (a * 100L)/b;
vz0
  • 32,345
  • 7
  • 44
  • 77
1

What you are doing right now is integer division. This will always return an integer/long result. You have to use a float or double to get a floating point result even if you cast it back to integer values afterwards.

long c = (long)(a/(float)b*100);
Dawnkeeper
  • 2,844
  • 1
  • 25
  • 41
1

The long c you are creating isn't the value you expected. It is in the integer range. To create longs, use

final long c= 3004230L * 6793368L; By adding an L after the number literal, you tell the compiler to compile it as a long instead of an int.

sona
  • 31
  • 7