This question already has answers here:
Integer division: How do you produce a double?
(11 answers)
Closed 7 years ago.
I wanna do this math in java:
int index = 3 * (9568/20001);
in my calculator it shows 3 *( 0.47837608... ) which is 1.43512824..
but, In Java that always give me 0, even I were trying use format, or java.lang.Math.round.
The first postion int 1 of 1.43512824 is what I want to get.
Try this
int index = (int)3 * (9568.0/20001);
Because an integer divided by an integer gives a integer in java thus your answer will not be accurate. If you write 9568.0/20001 it gives a double result and so result is more accurate.
Related
This question already has answers here:
java how to make user friendly percentage output from float number
(9 answers)
Closed 3 years ago.
How to round a number in Java?
Input: 0.655308; Expected output: 65.53.
Input: 1.0583104; Expected output: 105.83.
In power builder compute expression I use
act_qty *work_hour /
if (on_hour < work_hour ) /
sec_setm_gole_qty ,4)
How to run it in Java?
I would just multiply the value with 100
double roundedValue = value * 100;
System.out.printf("%.2f", roundedValue);
String.format("%.2f%%",value*100)?
This question already has answers here:
How does double to int cast work in Java
(3 answers)
Closed 4 years ago.
I understand that the result of Math.pow() is a double.
However, why isn't the below code causing an integer overflow when I have explicitly casted the result to an int?
Also, why is the result of both 'a' and 'b' the same i.e 2147483647
int a=(int)(Math.pow(2,377));
int b=(int)(Math.pow(2,32));
System.out.println(a);
System.out.println(b);
If a double is larger than the maximal int value, converting it to int will give you the maximal int value.
This question already has answers here:
Int division: Why is the result of 1/3 == 0?
(19 answers)
Closed 4 years ago.
I am trying to implement a simple equation in Java but keep getting the wrong answer apparently due to operator precedence which I am unable to understand.
The equation is:
NewMean = ((N-1) / N) * OldMean + (Xn / N)
in a simple example:
N = 6 ; OldMean = 6 ; Xn = 16
So,
NewMean = 5/6 * 6 + 16/6 = 7.6667 (Correct answer)
but in code implementation on Java i get wrong answer (2.6665):
double NewMean = ((N-1)/N)*oldMean + (Xn/N);
If the N variable is type int, then ((N-1) / N) is computed using integer division and will round 5/6 down to 0. Change N to a floating-point type and you should get the correct answer.
This question already has answers here:
"Integer number too large" error message for 600851475143
(8 answers)
Closed 4 years ago.
This is just a short question but I'm still curious. When I initialize a int variable with the highest value 2147483647 it's allright. but when I want to initialize a long and assign it to it's max value, it gives me a "number too large" error
int i = 2147483647;
long j = 9223372036854775807;
9223372036854775807 is an int literal, but is too large to fit into an int, hence the error. You can use a long literal instead by adding L to the end of it:
long j = 9223372036854775807L;
// Here --------------------^
This question already has answers here:
Division of integers in Java [duplicate]
(7 answers)
Closed 5 years ago.
I keep getting 0 with the following equation, and I'm sure it is something I am missing, but this has been bugging me for the past few days.
int BASE_SIZE = 8;
Point screenSize = new Point(1440,2000);
mMaxSize = mScreenSize.x/BASE_SIZE;
// This line is the line causing issue.
int surfaceViewSize = mMaxSize * ((BASE_SIZE-1)/BASE_SIZE);
This is regardless of if I make the variable an integer, if I use Math.round, I make it a double, anything. I can not for the life of me figure this out.
this integer division here:
(BASE_SIZE-1)/BASE_SIZE
result to be
int surfaceViewSize = mMaxSize * 0;
you need to cast one of the operands into a double or float
replace your operations with:
mMaxSize = 1.0*mScreenSize.x/BASE_SIZE;
int surfaceViewSize = mMaxSize * ((1.0*BASE_SIZE-1)/BASE_SIZE);
int surfaceViewSize = (mMaxSize * (BASE_SIZE-1))/BASE_SIZE;
Try this its just a braces issue