Java Divide Numbers Sample Code

Java programming language provides operators that allow you to do math operations hat perform addition, subtraction, multiplication, and division. Java defines these as Arithmetic Operators. We will focus on the division operator.

The division operator is a forward slash / which is found on the same key on your keyboard as the question mark. Keep in mind that anytime you divide a smaller number by a larger number the results in Java will be a 0, regardless if your dealing with an int, a long, or a double.


Divide Two Integers

Divide two numbers using the / division operator. It is optional whether or not your surround the operation in parentheses. In this case, the result calculation will be 500. If you divide two integers the result will always be an integer.

int resultInt = 10000 / 20;
or
int resultInt = (10000 / 20);

Result:
Result Int: 500


Divide Two Doubles

Dividing two doubles using the division operator. Again the parentheses are option but I suppose using them or not is a a matter of your specific coding style. If you one or both of the numbers being divided is a double the resulting calculation will be a double. The result may return up to 15 decimal places.

double resultDouble = 100000.00 / 1000.50;
or
double resultDouble = (100000.00 / 1000.50);

Result:
Result Double: 99.95002498750625


Divide Two Longs

Dividing two long numbers using the division operator. Notice we use the “L” to declare the literal as a long. The result of dividing two longs must be stored as an long result but the value returned will be an integer, which means the result will not be a decimal value.

long resultLong = 1234567891L / 987654L;
or
long resultLong = (1234567891L / 987654L);

Result:
Result Long: 1250


Cast Division Result to Double

So far we have seen that dividing two integers return an integer, dividing two doubles returns a double, and dividing two longs will return a long that is really an integer value. You can cast your result to a double if that is required for your programming application. This cast to double option will give you the decimal places.

double resultDouble = ((double) 1234567891L / 987654L);

Result:
Result Long to Double: 1250.0003958876287


Conclusion

Java does allows you to perform typical math operations with the Arithmetic Operators. Depending on what two numbers you are dividing, it is good to know what result you should be expecting based on how your Java application plans on using the result. Included below is the sample Java code created for this tutorial as well as the result output.



Java Divide Numbers Sample Code

package com.jcd.java.tutorials;

public class JavaDividingNumbers 
{

	public static void main(String[] args) 
	{
		
		int firstInt = 10000;
		int secondInt = 20;
		
		//Divide two integers to an integer without parentheses
		int resultInt = firstInt / secondInt;
		
		System.out.println("\nResult Int: "+resultInt);
		
		//Divide two integers to an integer without parentheses
		int resultInt1 = (100000 / 20);
		
		System.out.println("\nResult Int: "+resultInt1);
		
		/*------------------------------------------------*/
		
		double doubleOne = 100000.00;
		double doubleTwo = 1000.50;
		
		//Divide two doubles without parentheses returns a double
		double resultDouble1 = doubleOne / doubleTwo;

		System.out.println("\nResult Double: "+resultDouble1);
		
		//Divide two doubles with parentheses returns a double
		double resultDouble2 = (100000.00 / 1000.50);
		
		System.out.println("\nResult Double: "+resultDouble2);
		
		/*------------------------------------------------*/
		
		long longOne = 1234567891L;
		long longTwo = 987654L;
		
		//Divide two doubles without parentheses returns a double
		long resultLong1 = longOne / longTwo;

		System.out.println("\nResult Long: "+resultLong1);
		
		//Divide two doubles with parentheses returns a double
		long resultLong2 = (3004230 / 6793368);
		
		System.out.println("\nResult Long: "+resultLong2);
		
		double resultDouble3 = ((double) longOne / longTwo);
		
		System.out.println("\nResult Long to Double: "+resultDouble3);

	}

}



Java Divide Numbers Output

Result Int: 500

Result Int: 5000

Result Double: 99.95002498750625

Result Double: 99.95002498750625

Result Long: 1250

Result Long: 0

Result Long to Double: 1250.0003958876287

Add a Comment

Your email address will not be published. Required fields are marked *