Dennis Putnam wrote:For some reason using long truncated the decimal part.
Because long is an integral data type, not decimal.
Your code looks very wrong. I don't know what curr is, but using instanceof is not going to help you, because that's used for objects. Java is a strongly typed language, and doing type juggling in the hope that it will work out is not a good strategy.
Your input is a String. You want it to become a double. Just use Double.parseDouble().
However, double is a floating point type, and it's almost *NEVER* correct to use floating point types. If you need to store a decimal number accurately, you should be using BigDecimal instead of double.
Dennis Putnam wrote:Thanks. I needed to change it around and use double rather than long. For some reason using long truncated the decimal part.
Which I would kind of expect, because that code looks decidedly dodgy. Specifically, it WILL truncate the decimal part (and possibly also the integer part) if the type of 'curr' (whatever that is) isn't Double.
But having no idea of how JSON types - particularly "hybrid" ones like this - work, I can't really advise.
Why don't you explain to us WHAT you want to do, rather than how you're trying to do it.
It's usually the first step to better design.
Dennis Putnam wrote:I am parsing JSON output from a web site. One of the JSONObjects appears to be recognized as either double or long. I have no idea why/how its one or the other. They look the same to me when I print the JSONObject. Anyway, I need to add that value to a dataset for JFreeChart, which requires a double. If I convert the JSONObject to a double and it gets recognized as a long, I get an exception and vice versa. I need to somehow make sure that it is always converted to double regardless of which the JSONObject is.
Seems very odd to me, but here's my solution:and pass whatever JSON returns to that.
Of course it doesn't cover every eventuality - specifically: long integer values that are too large to store exactly in a double - but you haven't said what you want to do in those cases anyway.
And if JSON always returns an object (as opposed to a primitive), you may not even need the method - just pass;