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);
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:
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:
Simple division in Java - is this a bug or a feature?
(5 answers)
Closed 8 years ago.
I'm beginning to think my brain is playing a cruel joke on me, why on earth is 15/6 returning 2.0?
You can test here, http://ideone.com/xUaYEF.
double average = 15/6;
System.out.println("value: "+average);
And everytime, the output is,
value: 2.0
Last I checked with a calculator and google 15/6 is 2.5.
double average = 15/6.0;
Division of int by int will return an int. That is why you get 2. Then, as you declared it as a double, it would add the .0 at the end.
If you divide a double by double (15/6.0, 15.0/6, or 15.0/6.0) it will come out as a double.