Why does integer division code give the wrong answer?

二次信任 提交于 2019-11-26 08:54:12

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;
              ^

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;

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);

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;
	}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!