-2

It returns 0 if performed with plain BODMAS operation.

I have something like this:

int mbUsed=1;

int mbTotal=25;

int percent=(mbUsed/mbTotal)*100;
user1620220
  • 1,295
  • 8
  • 15

4 Answers4

1

1/25 will return 0, since int division can't return fractions.

You can cast to double for floating point division :

int percent=(int) ((double)mbUsed/mbTotal)*100;

Or if you want a more accurate result :

double percent = ((double)mbUsed/mbTotal)*100;

If you want to stay with int division, you can change the order of the operators :

int percent = (100*mbUsed)/mbTotal;
Eran
  • 387,369
  • 54
  • 702
  • 768
1

The int data type in Java contains whole values. You should instead store your values in the double or float data types, as they can contain decimal points.

Here you can see an example:

public static void main(String[] args) {

    int iVal1 = 1;
    int iVal2 = 25;
    int iVal3 = iVal1 / iVal2;

    System.out.println("Integer storage, int variables: " + iVal3);

    double dVal1 = iVal1 / iVal2;

    System.out.println("Double storage, int variables: " + dVal1);

    double dVal2 = (double) iVal1 / (double) iVal2;

    System.out.println("Double storage, double variables: " + dVal2);
}

Which outputs:

Integer storage, int variables: 0
Double storage, int variables: 0.0
Double storage, double variables: 0.04

Notice how the values you are dividing also have to have at least double precision. In my example I simply type cast them to a double (seeing as they are whole numbers, it will make no difference), but you could also store them in double data types as well.

Mike Elofson
  • 2,017
  • 1
  • 10
  • 16
0

You can also use BigDecimal, so for other ratio's You can manipulate scale: http://docs.oracle.com/javase/7/docs/api/java/math/BigDecimal.html This is especially recommended for currencies.

BigDecimal dividend= BigDecimal.ONE;
BigDecimal divisor = new BigDecimal(25);
BigDecimal result = dividend.divide(divisor );
maslan
  • 2,078
  • 16
  • 34
0

You might also consider just using doubles for your values mbUsed and mbTotal.

Example:

double mbUsed = 1;
double mbTotal = 25;
double percent = (mbUsed * 100)/mbTotal;

You could even add a decimal format, in case the numbers for mbUsed and mbTotal change.

Example:

DecimalFormat df = new DecimalFormat("#.00");

Then, you can use df.format(percent) in a System.out.println statement to display the percentage rounded to two decimal points.

Here is an example of some code that compiles as it should:

double mbUsed = 1;
double mbTotal = 25;
double percent = (mbUsed * 100)/mbTotal;   

DecimalFormat df = new DecimalFormat("#.00");

System.out.println("The number used: " + df.format(mbUsed));
System.out.println("The total number: " + df.format(mbTotal));
System.out.println("The percentage used is: " + df.format(percent) + "%.");

This outputs:

The number used: 1.00
The total number: 25.00
The percentage used is: 4.00%

I hope this works well for you!

Ryan
  • 511
  • 1
  • 8
  • 18