Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 3 years ago.
Improve this question
I'm trying to specifically use this value
new BigDecimal("23.025").setScale(2, RoundingMode.HALF_EVEN);
The output is 23.02, but should be 23.03. Is it a bug?
Just for curiosity DecimalFormat("#####0,00"); also does not work
Any other suggestion?
RoundingMode#HALF_EVEN JavaDoc says
Rounding mode to round towards the "nearest neighbor" unless both
neighbors are equidistant, in which case, round towards the even
neighbor
In your case the even neighbor is 02.
JavaDoc is important! Read it, always.
For RoundingMode, it can be found here (Java 8 version).
Related
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 12 months ago.
Improve this question
This Code is giving error that lossy conversion from double to int.
int[] ans=new int[1e5+1];
The concept is simple: 1E5 is a 'double' floating-point number in Java because the Java language specification says it is.
Meanwhile, an array size must be integral, and the compiler is telling you that. If you really want to use a floating-point number there, you need to cast to int. However, it's easier to just write 100000.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 7 years ago.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Improve this question
I'm new and would like to find and correct my errors. I got 2 errors while writing a java program to print prime nos. between 1 to 20.
code:
error:
; instead of ,
for (i=1;i<20;i++)
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
I feel like I'm missing something here, but I'm not entirely certain what and I'm relatively new so I'm having trouble searching for exactly what may be causing it.
java snippet follows:
double testDoublePi = 3.145926
System.out.printf("This one rounds: %.2f\tBut this does not: %.2f", testDoublePi, Math.PI );
"This one rounds: 3.15 But this does not: 3.14"
I expected both to be checking the thousandths digit and rounding accordingly, but that's apparently not the case.
I have a sneaking suspicion it has something to do with testDoublePi being finite vs Math.PI, but I'm not sure. I don't have a particular practical application that would require PI to be rounded offhand, but I figure it'd be important to know what's going on before I get myself in trouble and frustrated later.
What happens is that Pi is not 3.145926.... It is 3.1415926.... They are different:
3.145926 -> 3.15
3.1415926 -> 3.14
^
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
Why does this code evaluate to false?
code:
String[] a = {"donald,duck"};
String[] b = {"duck,donald"};
System.out.println(Arrays.asList(a).containsAll(Arrays.asList(b)));
output:
false
From the docs:
boolean containsAll(Collection c)
Returns true if this list contains all of the elements of the specified collection.
Update: Realized the flaw as soon as the first answer ticked in. I'll go and sit in the corner for a while now, thanks. *equips hat of shame*
Since "donald,duck".equals("duck,donald") is false, hence the result. You've 2 arrays with 1 elements each.
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
If this was a string and it was parsed as a double. Would java be able to process this as the expected value or would I need to change the format of these numbers? Would I need to remove the "+" or change e to "E"?
1.3870574e+01
The string parsed to a double just fine on my system.
See Double.valueOf(String str)