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.
Related
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:
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.
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:
Java rounding up to an int using Math.ceil
(15 answers)
Closed 6 years ago.
How do i get next large integer after division in java android
for example
11/5 = 2.2, wanted to round of this answer 3 not to 2
My calculations are complex and final answer, I need to show rounded of to next big integer value
Regards
Sujay
Use can use ceil method,
double ans = ((double) 11) / 5;
double result = Math.ceil(ans);
This question already has answers here:
Strange floating-point behaviour in a Java program [duplicate]
(4 answers)
Closed 8 years ago.
I want to know the reason why i am getting so many decimal points while doing this sum of double numbers
System.out.println(Double.parseDouble(".56"));
double dvalue=1.12+Double.parseDouble(".56");
System.out.println(dvalue);
output is as follows
0.56 and
1.6800000000000002
why second summation is adding those decimals it should simply 1.68
It's because the addition of the doubles 1.12 and 0.56 does not yield an exact representation of 1.68 (it doesn't precisely result in 1.68). You can see the difference with this code:
System.out.println(1.12+0.56); // 1.6800000000000002
System.out.println(1.68); // 1.68