32

I have a very simple division in Java (it's a product quantity / production per hour), however whenever I make this division I get strange errors:

float res = quantity / standard;

I have tried the above division with several values and I always get errors, however the one that I've tried everywhere else and gotten right was this:

Everywhere in the world:

13.6 = 6800 / 500;

Java:

13.0 = 6800 / 500;

I've researched BigDecimal and BigInteger, however I haven't found a way to create this division with them, is there any other way to do this division in Java without having precision errors??

Any help will be greatly appreciated.

Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130
Julian C.
  • 339
  • 1
  • 3
  • 4

4 Answers4

82

You're dividing integers, which means that you're using integer division.

In integer division the fractional part of the result is thrown away.

Try the following:

float res = (float) quantity / standard;
            ^^^^^^^

The above forces the numerator to be treated as a float which in turn promotes the denominator to float as well, and a float-division is performed instead of an int-division.

Note that if you're dealing with literals, you can change

float f = 6800 / 500;

to include the f suffix to make the denominator a float:

float f = 6800f / 500;
              ^
aioobe
  • 413,195
  • 112
  • 811
  • 826
  • 3
    Another common method is to multiply the numerator or denominator by 1.0. By the way, there is almost no reason to ever use float instead of double, unless you really care about saving 4 bytes. – Paul Tomblin Sep 02 '11 at 16:49
  • 4
    @Paul: and so there is no reason to ever use int instead of long, unless... [:-) – user85421 Sep 02 '11 at 21:37
  • @Carlos, maybe not in Java (actually, I don't know), but in C or C++, int can be considerably faster than long, whereas thanks to "math coprocessors" (some of remember when these were separate chips) doubles are often faster than floats because they don't have to be down converted. – Paul Tomblin Sep 02 '11 at 22:08
4

If you concerned about precision I would suggest using double which has more than double the number of digits of precision. However floating point only accurately represents fractions which are a sum or powers of 0.5. This means 0.6 is only approximately represented. This doesn't have to be a problem with appropriate rounding.

double d = (double) 6800 / 500;

or

double d = 6800.0 / 500;
Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130
  • Is there any perfomance differences between double and float??....I've been doing some light reading and I might be a little concerned regarding memory usage, the machine on which the software is going to be deployed is rather old – Julian C. Sep 06 '11 at 19:08
  • double uses 4 more bytes. If memory is very tight it can be worth using `float` and if you have a million of these, it could save 4 MB. If you don't have millions of these it probably not worth worrying about. – Peter Lawrey Sep 06 '11 at 20:47
1

In my case I was doing this:

double a = (double) (MAX_BANDWIDTH_SHARED_MB/(qCount+1));

Instead of the "correct" :

double a = (double)MAX_BANDWIDTH_SHARED_MB/(qCount+1);
Felipe Volpato
  • 334
  • 1
  • 5
-1

hi try this one it may help ful your requirement

double percent=(7819140000l-3805200000l)*100f/7819140000l;

public String format_Decimal(double decimalNumber) {
  NumberFormat nf = NumberFormat.getInstance();
  nf.setMaximumFractionDigits(5);
  nf.setMinimumFractionDigits(2);
  nf.setRoundingMode(RoundingMode.HALF_UP);
  String x = nf.format(decimalNumber);
  return x;
 }
Chandu D
  • 505
  • 3
  • 17