This question already has answers here:
Integer division: How do you produce a double?
(11 answers)
Closed 7 years ago.
So I am writing a program in which I am using a Double, but java is not using the decimal point. For example:
I have
double F;
F = 9/5;
Instead of java giving me 1.8 as an answer, it is just giving me back 1.0
Thanks for all the help
Change it to
F = (double)9/5;
because 9/5 will give int which then converted to double giving you 1.0.
When you write
F = 9/5;
Java performs the integer division of 9 and 5, which is 1. Because the destination variable F is a float, Java converts the 1 into the equivalent 1.0. If you want it to perform a floating-point division, use 9.0/5. That way, the 5 is promoted to the equivalent 5.0, and the division is thus 9.0/5.0=1.8.
Related
This question already has answers here:
Beginners Java Question (int, float)
(4 answers)
Division of integers in Java [duplicate]
(7 answers)
Closed 2 years ago.
If I divide a double type variable, the Decimal part becomes zero.
a=13122/10;
System.out.println (a);
Prints
1312.0
As you can see, the Decimal part became zero when I divided it.
But I need the value
1312.2
Your problem is that while you may have stored "a" as a double, you are really dividing two "ints" and saving that. When you divide 2 ints, the number automatically gets rounded down. So, it's rounded down to 1312.0.
What you need is this,
a = (double)13122/10;
or this:
a = 13122.0/10;
You are dividing integers. You can cast the division and then you are ok
double a = (double) 13122/10;
System.out.println (a);
This question already has answers here:
Integer division: How do you produce a double?
(11 answers)
Java integer-double division confusion [duplicate]
(5 answers)
Closed 4 years ago.
I think 5 is an int. But there are no variables. I don't know why the output is a double. I tried in Netbeans already.
Multiplication and division are done left to right (and regardless, you put parentheses around the first operation), and if you divide any primitive umber by a double, you will get a double out of it.
5/2.0 will clearly be evaluated first.
So, even though ‘5’ is an int, if you do 5/2.0, you’ll get a double (namely, 2.5). Now, at this point, your code is already working with a double.
When you multiply 2.5 by 2, you then output 5.0, since 2.5 is a double.
This question already has answers here:
is there anyway to convert from Double to BigInteger?
(3 answers)
Closed 5 years ago.
How do you convert a double to a BigInteger, as accurately as possible? BigInteger doesn't have a valueOf(double) method. It does have a valueOf(long) method, but converting via long, would destroy any value outside the 2^64 range, and in particular I'm trying to get large numbers like 1e100 to convert to their corresponding integer values to the sixteen or so significant figures allowed by floating point precision.
You can convert the double into a BigDecimal and then into a BigInteger:
BigInteger k = BigDecimal.valueOf(doublevalue).toBigInteger();
You could use BigDecimal.valueOf(double) and then call toBigIntegerExact() on that. Like,
BigInteger bi = BigDecimal.valueOf(Math.PI).toBigIntegerExact();
This question already has answers here:
Simple division in Java - is this a bug or a feature?
(5 answers)
Closed 7 years ago.
Found below output strange. Is it really?
double rate = 11/12; // outputs 0.0
double rate = 11.00/12; // outputs 0.916666667;
Why so much difference?
In the 1st case first division is done so an int divided by an int gives a integer i.e, 11/12=0 Then this integer is converted to double .ie, 0.0. In the 2nd case a double(11.00) is divided by a integer(12).The integer 12 is then automatically type casted to double as division should occur with similar types. This division gives a double value which is exact in reality(0.916666667)
See this link https://docs.oracle.com/javase/specs/jls/se7/html/jls-5.html
This question already has answers here:
Raising a number to a power in Java
(10 answers)
Closed 8 years ago.
I am trying to calculate the power as below but it is giving me 'bad operands type for binary operator '^'. I am guessing that it is a precedence issue but it still doesn't fix with inserting additional brackets
double pw = ((N - (df + 1))^2);
You should use java.lang.Math.pow(x,y)
Example: java.lang.Math.pow(2,3) returns 8
See this
http://www.tutorialspoint.com/java/lang/math_pow.htm
java.lang.Math.pow(double a, double b)
You can use static import for this.