1

I am facing an issue in multiplication and division:

float value =  (100 / 846) * 500;
Log.i(TAG,"value = " + value);

Every time this float value i slogged as 0.0

What is the problem?

rds
  • 26,253
  • 19
  • 107
  • 134
NagarjunaReddy
  • 8,621
  • 10
  • 63
  • 98
  • possible duplicate of [noob: Why divide always produces 0.0 float/integer problem?](http://stackoverflow.com/questions/3779604/noob-why-divide-always-produces-0-0-float-integer-problem) – rds Mar 27 '14 at 09:23
  • I was only twitting yesterday wondering why people forget primary school maths like integer division and remainder. – Peter Lawrey Mar 27 '14 at 09:29

4 Answers4

4

100 / 846 is an integer division and yields 0. Use (100.0 / 846.0) * 500.

Stefan
  • 4,645
  • 1
  • 19
  • 35
4

100 is not a float, it's an int. Use 100f, 846f and 500f for floats.

rds
  • 26,253
  • 19
  • 107
  • 134
1

You're performing integer division and multiplication and storing that result in a float variable. use float literals instead. E.g. 100f

nfusion
  • 589
  • 1
  • 4
  • 11
1

Replace your code with:

float value =  (100F / 846F) * 500F;   
Log.i("==========","  value == > " + value); 
Himanshu Agarwal
  • 4,623
  • 5
  • 35
  • 49