1

Possible Duplicate:
Why does (360 / 24) / 60 = 0 … in Java

I am having this problem:

float rate= (115/100);

When I do:

System.out.println(rate);

It gives me 1.0 What... is the problem?

Community
  • 1
  • 1
TheNotMe
  • 1,048
  • 2
  • 17
  • 33

7 Answers7

10

115 and 100 are both integers, so will return an integer.

Try doing this:

float rate = (115f / 100f);
cowls
  • 24,013
  • 8
  • 48
  • 78
  • 2
    more elegant solution than adding a .0 imo – MarioDS Dec 21 '12 at 15:45
  • 2
    @MarioDeSchaepmeester - even more than elegant, adding a .0 creates a `double` which then gets converted to `float`. The correct solution is using `f` is you actually want a `float`. – Brian Roach Dec 21 '12 at 15:48
  • for me, 2 is clearer, but it doesn't matter either way – cowls Dec 21 '12 at 15:53
  • @BrianRoach Indeed I thought so, but I wasn't sure. I'm not too aware of these kind of details. More important is to know why the OP's code doesn't produce the result he expected. – MarioDS Dec 21 '12 at 16:00
3

You're performing integer division (which provides an integer result) and then storing it in a float.

You need to use at least one float in the operation for the result to be the proper type:

float rate = 115f / 100;
Justin Niessner
  • 242,243
  • 40
  • 408
  • 536
2
float rate= (115/100);

Does the following things:

1) Performs integer division of 115 over 100 this yields the value 1.
2) Cast the result from step 1) to a float. This yields the value 1.0

What you want is this:

float rate = 115.0/100;

Or more generally, you want to convert one of the pieces of your division into a float whether that is via casting (float)115/100 or by appending a decimal point to one of the two pieces or by doing this float rate = 115f / 100 is completely up to you and yields the same result.

Woot4Moo
  • 23,987
  • 16
  • 94
  • 151
2

In order to perform floating-point arithmetic with integers you need to cast at least one of the operands to a float.

Example:

int a = 115;
int b = 100;
float rate = ((float)a)/b;
dogbane
  • 266,786
  • 75
  • 396
  • 414
1

use float rate= (float)(115.0/100); instead

gefei
  • 18,922
  • 9
  • 50
  • 67
1

It is enough to put float rate = 115f / 100;

Adrian Ber
  • 20,474
  • 12
  • 67
  • 117
0

The problem you have is that your dividend and divisor are declared as integer type.

In mathematic when you divide two integer results only with remainder. And that is what you assign to your rate variable.

So to have the result as you expected, a remainder with fraction (rational numbers). Your dividend or divisor must be declared in a type with precision.

Base two known types with precision are float (Floating point) and double (Double precision).

By default all numbers (integer literals for purists) written in Java code are in type int (Integer). To change that you need to tell the compiler that a number you want to declare should be represent in different type. To do that you need to append a suffix to integer literal.

Literals for decimal types:

float - f or F; 110f;

double - d or D 110D;

Note that when you would like to use the double, type you can also declare it by adding a decimal separator to literal:

double d = 2.; or double d = 2.0;

I encourage you to use double type instead of float. Double type is more suitable for most of modern application. Usage of float may cause unexpected results, because of accuracy problem that in single point calculation have bigger impact on result. Good reading about this “What Every Computer Scientist Should Know About Floating-Point Arithmetic”. In addition on current CPU architecture both float and double have same performance characteristic. So there is not need to sacrifice the accuracy.

A final note about floating point types in is that non of them should be use when we write a financial application. To have valid results in this matter, you should always used [BigDecimal]

  • I agree that almost always double is a better choice than float, but what is "accurate enough" depends on the application. In some cases, float is indeed "accurate enough". – Patricia Shanahan Dec 21 '12 at 16:05
  • @PatriciaShanahan, For sure you have right with that. But only reason to use float over double was that on old CPU it was faster. So this type was used naturally in performing 3D calculation where precision is not so important. Currently the architecture was improved and this type can be treated as historical. – Damian Leszczyński - Vash Dec 21 '12 at 17:13
  • Compute performance is indeed largely irrelevant. Once data meets instructions in an ALU, everything runs fast. The problem is getting the data to and from the ALU, via finite capacity caches and finite bandwidth data transfer paths. Float may still be relevant for applications that use very large quantities of low precision floating point data. For those applications, using float effectively doubles the sizes of data caches and the bandwidth of their data transfer paths. Of course, those applications are not typically written in Java. – Patricia Shanahan Dec 21 '12 at 21:41