This question already has answers here:
Is floating point math broken?
(31 answers)
Java double precision sum trouble
(2 answers)
Closed 6 years ago.
When I am trying to multiply 3 (Integer type) with 112.1 (Double) then the result I get contains many decimal points.
Integer a=3;
Double b=112.1;
Double result=a*b = 336.29999999999995
But when we multiply 2 (Integer) with 112.1 (Double) then decimal point is so minimal.
Integer a=2;
Double b=112.1;
Double result=a*b = 224.2
Can anybody give me the reason why this strange behavior comes?
You can't store actual decimal representation of fractions.
This is because computer use binary to save any value, including fractional values.
Therefore, there is some difference between actually saved one and what you want to store.
Related
This question already has answers here:
Int division: Why is the result of 1/3 == 0?
(19 answers)
How to make the division of 2 ints produce a float instead of another int?
(9 answers)
Division of integers in Java [duplicate]
(7 answers)
Closed 5 years ago.
So I'm trying to do math on ints stored in an array of ints.
float day1Hours = (day1[3]-day1[2]) / 2;
for this specific problem, day1[3] = 19 and day1[2] is 10. So, it should be doing 19-10 = 9, and then dividing that by 2 to make 4.5. But, the output that I am getting is 4. I've also tried storing day1Hours as a double but that made no difference. How would I make this be able to do the math correctly and get those decimal values that I need?
The problem is that you are doing integer division and then converting to a float. Try
float day1Hours = (day1[3]-day1[2]) / 2.0f;
Using a float literal in the denominator will cause the division to be done in floating point, and you won't get integer truncation. (As an alternative to using a float literal, you could cast the numerator or denominator to a float, but that seems somewhat baroque to me. It would be more suitable if both the numerator and denominator were int variables.)
The reason that just changing the type of day1Hours doesn't affect the problem is that the entire right side is evaluated first using the declared data type of day1 and then converted to whatever type is on the left of the assignment.
float day1Hours = (float)(20-9) / 2; //5.5
Problem is that on the right side of equation the numbers are integers and when dividing 2 integers the decimal places are truncated (integer division rounds towards zero) 4.5 -> 4.0.
Try changing 2 -> 2f so the 2 would be considered a float instead of an integer.
This question already has answers here:
Division of integers in Java [duplicate]
(7 answers)
Why does integer division code give the wrong answer? [duplicate]
(4 answers)
Best way to Format a Double value to 2 Decimal places [duplicate]
(2 answers)
Closed 5 years ago.
I try this way to display it.
DecimalFormat REAL_FORMATTER = new DecimalFormat("0.0###############");
double value = 691 / 3600000;
textView1.setText(REAL_FORMATTER.format(value));
What's the problem?
All the time TextView shows: 0,0. I thought it should be
0,000191944.
This solve may cause problem with number biger than 10. I need
display double value and I don't know how do it.
When dividing in Java the default value is int. You can try dividing using this way:
double value = 691 /3600000D
double value = (double)691/3600000;
or when divided with two places precision:
double value = 691.0/36.0
This question already has answers here:
How to round a number to n decimal places in Java
(39 answers)
Closed 6 years ago.
I have a scenario where we need to display two zeros i.e 00 after the precision for double value. But unfortunately Java is not returning the second zero.
I have tried the below way but it is returning only one zero after decimal.
double inputvalue = 12345.00;
double d = ((BigDecimal.valueOf(inputvalue)).setScale(2,RoundingMode.UP)).doubleValue();
Expected output: 12345.00 but it is returning 1234.0.
Have you considered using a string to display the value. e.g.
String.format( "%.2f", d)
This question already has answers here:
Division of integers in Java [duplicate]
(7 answers)
Closed 7 years ago.
I used the following code:
double pow = 3/7;
double num = 85;
System.out.println(Math.pow(num, pow));
Expected result:
6.71...
The output is
1.0
Any idea why?
3/7 is evaluated to 0, since you are dividing two integers, so Math.pow(num, pow) becomes Math.pow(num, 0.0) which is 1.0.
Change it to 3.0/7 in order to get a floating point result.
This question already has answers here:
Division of integers in Java [duplicate]
(7 answers)
Closed 7 years ago.
I used the following code:
double pow = 3/7;
double num = 85;
System.out.println(Math.pow(num, pow));
Expected result:
6.71...
The output is
1.0
Any idea why?
3/7 is evaluated to 0, since you are dividing two integers, so Math.pow(num, pow) becomes Math.pow(num, 0.0) which is 1.0.
Change it to 3.0/7 in order to get a floating point result.