Division of Integers in Java

Division of integers in Java

Converting the output is too late; the calculation has already taken place in integer arithmetic. You need to convert the inputs to double:

System.out.println((double)completed/(double)total);

Note that you don't actually need to convert both of the inputs. So long as one of them is double, the other will be implicitly converted. But I prefer to do both, for symmetry.

Integer division in Java

They are being divided in integer arithmetics. So dividing integer a by integer b you get how many times b fits into a. Also a % b will give you a remainder of a division. So (a / b ) * b + a % b = a

Java - How to do floor division?

You can do

double val = 5 / 2;
int answer = Math.floor(val);

OR

int answer = Math.floorDiv(5, 2);

If you were to call System.out.println(answer); the output would be

2

Division of integers and its assignment

In your first example, 23/4 returns 5 because that is what integer division does: p/q returns the largest integer i such that i*q <= p. There is no casting going on here!

In your second example, 23.0/4.0 returns 5.75 because this is how floating-point division works: when p and q are floats, p/q returns the most precise representation of f available such that f * q = p.

In a case you don't consider, 23.0/4 would also return 5.75. In this case, there is casting. Since p and q are of different types, float and int, the lower type is promoted without loss of precision and the division proceeds as if p and q were of the higher type all along.

In the case you're concerned about, it is always possible to use an int to assign a value to a higher-typed variable (long, float, or double). What happens here is integer division, followed by promotion.

In order to force floating-point division, cast one of p or q to floating-point:

public double divide(int p, int q) {

return (double)p/q;
}

Int division: Why is the result of 1/3 == 0?

The two operands (1 and 3) are integers, therefore integer arithmetic (division here) is used. Declaring the result variable as double just causes an implicit conversion to occur after division.

Integer division of course returns the true result of division rounded towards zero. The result of 0.333... is thus rounded down to 0 here. (Note that the processor doesn't actually do any rounding, but you can think of it that way still.)

Also, note that if both operands (numbers) are given as floats; 3.0 and 1.0, or even just the first, then floating-point arithmetic is used, giving you 0.333....

Java Integer (java.lang.Integer) Array division and other math operations

You can do it pretty concisely with Java Streams:

Arrays.stream(FixedDays).map(i -> i / 365.0).toArray();

or

double[] doubles = Arrays.stream(FixedDays)
.map(i -> i / 365.0)
.mapToDouble(Double::doubleValue)
.toArray();

or

Double[] Ts = Arrays.stream(FixedDays)
.map(i -> i / 365.0)
.toArray(Double[]::new);

Java different situation division

Try casting the float to an int and compare the results, if it's the same, then print the integer, otherwise, use the float:

public class Main {
public static void main(String[] args) {
int a = 6;
int b = 3;
getDivisionResult(a, b);
b = 4;
getDivisionResult(a, b);
}
private static void getDivisionResult(int a, int b) {
float div = (float) a / b;
int rounded = (int) div;
if(rounded == div)
System.out.println(rounded);
else
System.out.println(div);
}
}

Output:

2
1.5

How to divide two ints and return a double in Java

(((double)a/(double)b)) This will give you = 0.57 and then this (int) this will convert 0.57 to 0 here (int)(((double)a/(double)b)); because integer can only hold whole numbers hence decimal values will be truncated.

use this to keep decimal values

double correctAnswer = (((double)a/(double)b));

Update : To achieve the decimal result, only one operand need to be cast as double and the optimizations for second parameter will be done by compiler.

Update Credits : @stackoverflowuser2010 and @cricket_007.

double correctAnswer = (double)a/b; 

or

This can also be done without explicit casting with Type Conversion or Type Casting which is done by the compiler.

Example Credits : @Andreas

double correctAnswer = a;  // a , will automatically converted to double 
correctAnswer /= b;

How to make the division of 2 ints produce a float instead of another int?

Just cast one of the two operands to a float first.

v = (float)s / t;

The cast has higher precedence than the division, so happens before the division.

The other operand will be effectively automatically cast to a float by the compiler because the rules say that if either operand is of floating point type then the operation will be a floating point operation, even if the other operand is integral. Java Language Specification, §4.2.4 and §15.17

Dividing two integers and rounding up result to nearest integer

Since a and b are integers, a/b will use integer division, and only return the "whole" part of the result. Instead, you should multiply a by 100.0 (note the .0, which makes it a double literal!) to use floating-point division, and then ceil the result, and truncate it to an int:

c = (int) Math.ceil(100.0 * a / b);


Related Topics



Leave a reply



Submit