I know that in Python you can do floor division like this:
5 // 2 #2
The // is used for something totally different in Java. Is there any way to do floor division in Java?
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
You can easily use Math.floorDiv() method.
For example:
int a = 15, b = 2;
System.out.println(Math.floorDiv(a, b));
// Expected output: 7
If you're using integers in the division and you cast the solution to another integer (by storing the result in another integer variable), the division is already a floor division:
int a = 5;
int b = 2;
int c = 5/2;
System.out.println(c);
>> 2
As khelwood stated, it also works with negative values but the rounding is done towards 0. For example, -1.7 would be rounded to -1.
use floorDiv()
int x = 10;
int y = 3;
System.out.println(Math.floorDiv(x,y));
Related
I want to calculate the increase of percentage of a variable from type int while using another variable from type int for the percentage (50 percent).
thanks in advance for anyone who is willing to help.
`
int a = 3;
int percentage = 3 / 2;
// here I get 3 instead of 4 which is the required answer.
a = a * percentage;
System.out.println(a);
// but here I get the required answer normally.
a = 3;
a = a * 3 / 2;
System.out.println(a);
`
"Percentage" is just a weird of "this value that generally is between 0 and 1 should be rendered by multiplying by 100 and adding a % symbol afterwards". In other words, it's purely a way to display a thing. 50% means 0.5.
int cannot represent 0.5. double sort of can (double and float aren't perfectly accurate). In addition / is integer division if both the left and right side are ints. So, we need to do a few things:
int a = 3;
double b = 1.0 * 3 / 2; // without that 1.0 *, it wouldn't work
System.out.println(b); // prints "1.5"
double c = a * b;
System.out.println(c); // prints 4.5.
int d = ((int) (a * b) + 0.1);
System.out.println(d); // prints 4
Because doubles aren't entirely accurate, and (int) rounds down, adding a small delta (here, 0.1) is a good idea. Otherwise various values will surprise you and go wrong (say, your math ends up at 3.99999999, solely because double is not perfectly accurate, then casting that to int gets you a 3).
This question already has answers here:
Why does integer division code give the wrong answer? [duplicate]
(4 answers)
Closed last year.
Division of two numbers using arithmetic operators in Java.
Scanner scan = new Scanner(System.in);
System.out.print("Write a number: ");
String mataett = scan.next();
System.out.print("Write a number: ");
String matatva = scan.next();
int nr1 = Integer.parseInt(mataett);
int nr2 = Integer.parseInt(matatva);
System.out.println(Math.round(nr1 / nr2*10.0)/10.0);
When I run this with 73/27 I get the answer 2.0, but what I want to get is the answer 2.7
When I run this with 73/27 I get the answer 2.0, but what I want to
get is the answer 2.7
The reason being you're getting the unexpected result is because when you divide by integers, the result is always an integer, which means any number after the decimal point is truncated.
nr1 / nr2 // <-- this part within the calculation is causing the problem.
A simple trick which you can do to overcome the problem is to multiply one of the integers nr1 or nr2 with 1.0 first then that should produce a double which should allow you to keep the precision because you're dividing by double now rather than int.
Example:
System.out.println(Math.round((1.0 * nr1) / nr2 * 10.0)/10.0);
Notice the 1.0
Change the line
System.out.println(Math.round(nr1 / nr2*10.0)/10.0);
To the line
System.out.println(Math.round(nr1 / ((double)nr2)*10.0)/10.0);
Because when you divide two integers, the answer is always an integer which is always rounder down ((int)2.9 = 2 and (int)3.1 = 3). cast into doubles and then divide in order to get the right answer.
Let's look at the following code:
int a = 10;
int b = 3;
System.out.println(a/b);
This will print 3, it will not print 3.3333333... that is because when dividing integers the result is always an integer and always rounded down. There are several ways to solve this
int a = 10;
double b = 3;
System.out.println(a/b);
This will print 3.3333333... because we are no longer dividing 2 integers, one of the variables is a double and therefor the result will be a double
Other ways:
Cast into a double:
int a = 10;
int b = 3;
System.out.println((double)a/b);
Multiplying an integer with a double will result in a double and therefor this will also work
int a = 10;
int b = 3;
System.out.println(1.0*a/b);
Notice that this will not work
int a = 10;
int b = 3;
System.out.println(a/b * 1.0);
This will not work because the division will be calculated before the multiplication and you will get the result 3.0 .
Try the following:
double nr1 = Integer.parseInt(mataett);
double nr2 = Integer.parseInt(matatva);
int a = 1;
int b = 10;
int c = 3;
int d = (1/10)*3
System.out.println(d)
Result: 0
How do i make this Calculation work ?
and round up or down ?
It should be:
(1/10)*3 = 0.1 * 3 = 0.3 = 0 and
(4/10)*3 = 0.4 * 3 = 1.2 = 1
Thanks a lot!
1 / 10
This is integer division and as integer division the result is 0.
Then 0 * 3 = 0
You can use double literals:
1.0 / 10.0
Try:
int d = (int) (((double)4/10)*3);
1/10
This line return 0.so 0*3=0.Use double instead of int
as both a and b are integer so the output will also be int which makes 1/10 as 0 and then 0*3=0
You will want to perform the calculation using floating-point representation. Then you can cast the result back to an integer.
I note that you refer in your question to rounding up/down.
Math.round() will help you here.
Returns the closest long to the argument. The result is rounded to an
integer by adding 1/2, taking the floor of the result, and casting the
result to type long.
This will works
int a = 1;
int b = 10;
int c = 3;
int d = (1*3/10);
System.out.println(d);
public class TestSample {
public static void main(String[] args) {
int a = Integer.parseInt(args[0]);
int b = Integer.parseInt(args[1]);
double ran = Math.random();
}
}
I don't want to use Random r = new Random(); class. Is there any other way to generate random numbers. I am just struck with what logic could be applied to generate random numbers between two numbers.
It's really easy... you only need to figure out which is the minimum value and what is the difference between the two numbers (let's call it diff). Then, you can scale the Math.random value (between 0 and 1) by multiplying by diff (now its range is between 0 and diff). Then, if you add the minimum value, your range is between min and min + diff (which is the other value)
int min = min(a,b);
int max = max(a,b);
int diff = max - min;
int result = min + diff * Math.random();
Consider using this code:
int a = Integer.parseInt(args[0]);
int b = Integer.parseInt(args[1]);
double ran = Math.random();
double random;
if(a < b)
random = (b-a)*ran + a;
else
random = (a-b)*ran + b;
This will work for a >= 0 and b >= 0 if you consider using negative number the logic sligtly changes
If you are expecting a double result, the simplest approach is
int a =
int b =
double result = (a-b)*Math.random() + b;
It doesn't matter which is greater as you get the same distribution.
However, if you want a random integer between 'a' and 'b' is a bit more complex.
int a =
int b =
int result = Math.floor((Math.abs(a-b)+1) * Math.random()) + Math.min(a, b);
The reason the result is different is that a random double between 0 and 1 will be just less than 1 i.e. [0.0, 1.0) However a random integer between 1 and 6 usually includes 1, 2, 3, 4, 5, 6 equally. As a decimal this is the round down of [0.0 ... 7.0)
You may get white noise from your microphone, and take any number from there. After that you may take any number from the given data, and do with it what you want. The example of getting data from the microphone can be found here.
I need to cast a double to an int in Java, but the numerical value must always round down. i.e. 99.99999999 -> 99
Casting to an int implicitly drops any decimal. No need to call Math.floor() (assuming positive numbers)
Simply typecast with (int), e.g.:
System.out.println((int)(99.9999)); // Prints 99
This being said, it does have a different behavior from Math.floor which rounds towards negative infinity (#Chris Wong)
To cast a double to an int and have it be rounded to the nearest integer (i.e. unlike the typical (int)(1.8) and (int)(1.2), which will both "round down" towards 0 and return 1), simply add 0.5 to the double that you will typecast to an int.
For example, if we have
double a = 1.2;
double b = 1.8;
Then the following typecasting expressions for x and y and will return the rounded-down values (x = 1 and y = 1):
int x = (int)(a); // This equals (int)(1.2) --> 1
int y = (int)(b); // This equals (int)(1.8) --> 1
But by adding 0.5 to each, we will obtain the rounded-to-closest-integer result that we may desire in some cases (x = 1 and y = 2):
int x = (int)(a + 0.5); // This equals (int)(1.8) --> 1
int y = (int)(b + 0.5); // This equals (int)(2.3) --> 2
As a small note, this method also allows you to control the threshold at which the double is rounded up or down upon (int) typecasting.
(int)(a + 0.8);
to typecast. This will only round up to (int)a + 1 whenever the decimal values are greater than or equal to 0.2. That is, by adding 0.8 to the double immediately before typecasting, 10.15 and 10.03 will be rounded down to 10 upon (int) typecasting, but 10.23 and 10.7 will be rounded up to 11.
(int)99.99999
It will be 99.
Casting a double to an int does not round, it'll discard the fraction part.
Math.floor(n)
where n is a double. This'll actually return a double, it seems, so make sure that you typecast it after.
This works fine int i = (int) dbl;
new Double(99.9999).intValue()
try with this, This is simple
double x= 20.22889909008;
int a = (int) x;
this will return a=20
or try with this:-
Double x = 20.22889909008;
Integer a = x.intValue();
this will return a=20
or try with this:-
double x= 20.22889909008;
System.out.println("===="+(int)x);
this will return ===20
may be these code will help you.
Try using Math.floor.
In this question:
1.Casting double to integer is very easy task.
2.But it's not rounding double value to the nearest decimal. Therefore casting can be done like this:
double d=99.99999999;
int i=(int)d;
System.out.println(i);
and it will print 99, but rounding hasn't been done.
Thus for rounding we can use,
double d=99.99999999;
System.out.println( Math.round(d));
This will print the output of 100.