Math.pi vs Double and rounding via printf in java [closed] - java

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
^

Related

1e5 which is equivalent to 10^5 . Is this value represents double or int in java. Can You please elaborate the concept [closed]

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.

Java Big Decimal Half Even Bug? [closed]

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

Random.nextInt seems wrong from a book [closed]

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 4 years ago.
Improve this question
It's a block of code for random verification code in a Servlet book.
the underlined part must be a number of 30+?
but the intention is one of those chars above
Pic
NextInt method only has one parameter which is the max number (exclusive)?
So is it a mistake in the book?
nextInt(int) returns a [pseudo]random number between 0 and the parameter passed to it, exclusive. Thus, if you pass a length of an array, you'd get a random valid index from the array, which you can then use to pick a random element from it, as this code does.

Strings in ternary operators interacting with numbers [closed]

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 I have an if statement that includes a string like this:
double GUInumber1 = ((GUInumber1 >= 0 || <= 0)? Double.parseDouble(GUIfirstNumber) : 0);
('GUIfirstNumber' is the string)
Why does it come up as a error? Do I need brackets somewhere, do I need to use 'If/else' instead?
Supposedly my compiler says it does not recognize or (||), is this supposed to be something else or does this work in a completely different way.
Any help on this situation would be appreciated, also, if you need to know or are just wondering why I want to make a if statement for a string its because whenever I try to put in a letter instead of a number Java crashes, I'm hoping I could get this to alternatively solve the situation instead.
EDIT:
Solved, had to remove 'or' statements and alternatively make more else statements instead.
This expression (GUInumber1 >= 0 || <= 0) is not correct.
You will need something on both sides of the <= operator.

Scientific Notation as a Double java Android [closed]

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)

Categories