java double number division giving strange output [duplicate] - java

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

Related

How to get Decimal part in Double variable Division[JAVA] [duplicate]

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);

why is the output of System.out.println((5/2.0)*2); is 5.0? [duplicate]

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.

Java is not using the decimals [duplicate]

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.

Not getting correct answers for sum of two double value [duplicate]

This question already has answers here:
Is floating point math broken?
(31 answers)
Why do I see a double variable initialized to some value like 21.4 as 21.399999618530273?
(14 answers)
Closed 7 years ago.
I have below program:
double tax = 180.09;
double gTotal = 277.08;
double total = tax + gTotal;
System.out.println(total);
After execution it outputs 457.16999999999996.
After Google search I found that one of the cause for this behavior is that there may be no precise binary representation of the fraction part.
To verify that I tried below:
double dNum = 457.17;
System.out.println(dNum);
It gave correct output as 457.17. In this case, why it is not printing 457.16999999999996.

Why do I receive unhelpful results for subtractions? [duplicate]

This question already has answers here:
Why do I see a double variable initialized to some value like 21.4 as 21.399999618530273?
(14 answers)
Closed 9 years ago.
double foo = 3;
double bar = 2.1;
System.out.println(foo - bar + "");
Output:
0.8999999999999999
Why? Is this some Java joke, which is not understandable for mere mortals?
It isn't a joke. It is floating-point precision error problem. The main gist of this problem is that floating points are represented in base 2 rather than 10, and that the precision of doubles is not arbitrary.
If you want precision, you can use BigDecimal class:

Categories

Resources