This question already has answers here:
Raising a number to a power in Java
(10 answers)
Closed 8 years ago.
Could anyone explain to me why
System.out.println(100*(1-10^(-10/10)));
results in the number "800" being printed out? The correct answer is 90 when you use a calculator. How would I go about doing this calculation in Java?
Thanks!
The ^ operator does not do what you think it does. It is bitwise-xor
You need to look into the Math.pow() method.
Related
This question already has answers here:
How can I check if multiplying two numbers in Java will cause an overflow?
(15 answers)
Closed 1 year ago.
int ans = Integer.MAX_VALUE -(-1); //should I explicitly cast my method parameters in calculation to a wider bit type ?
Found a solution on searching the internet. An article that may help learners like me.
https://www.drdobbs.com/jvm/signalling-integer-overflows-in-java/210500001
This question already has answers here:
Generate a random double in a range
(7 answers)
Using Math.round to round to one decimal place?
(9 answers)
Closed 4 years ago.
I am trying to round to the nearest decimal value, however, this line of code keeps returning a number between 0 and 1, I also want the output to be between 1 and 10. Where am I going wrong?
power[i] = rng.nextDouble();
Math.round(ThreadLocalRandom.nextDouble(1,10)*10)/10.0
You can use JDK's Math.round.
A detailed example can be found here.
This question already has answers here:
Why does floating-point arithmetic not give exact results when adding decimal fractions?
(31 answers)
Retain precision with double in Java
(24 answers)
Closed 5 years ago.
Can anyone explain what's going on here? My calculator insists that the result of equitation is 13201.
double test = 132.01/0.01D;
System.out.println(test); <- 13200.999999999998
Probably, this is very simple question, but I really don't understand. Most of questions in SO on similar topic involves Integer and Double.
This question already has answers here:
Why does integer division code give the wrong answer? [duplicate]
(4 answers)
Closed 6 years ago.
I am doing simple calculation in java. Expected result is 51.3348 but what I am getting is 51.0, here is my calculation
float percent = (7819140000l-3805200000l)*100/7819140000l;
Is that problem with datatype? How can I resolve this to get value as 51.3348
Thanks in Advance
add an f to one of the values:
float percent = (7819140000l-3805200000l)*100f/7819140000l;
if yiu do not do it, Java will make a devision by long values
This question already has answers here:
Best way to Format a Double value to 2 Decimal places [duplicate]
(2 answers)
Closed 7 years ago.
The title says it all. Right now if I input a number like 100.50, in my program it prints as 100.5. Is there an easy way to make the program recognize the zero?
You can do this trick.
String s = String.format("%.2f", 100.50);