• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Long/Double Conversion Problem

 
Ranch Hand
Posts: 377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am extracting data from a web site in JSON format. There is one value that is inconsistent and can be either long or double. How can I tell which it is and make sure it is converted to long? TIA.
 
Saloon Keeper
Posts: 15526
364
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Depends on what you want to do with the fractional part. If you just want to discard it, then you can first treat the value as a String and then use a regex pattern to find only the integral part. After that, use Long.parseLong() to convert the integral part to a long.
 
Dennis Putnam
Ranch Hand
Posts: 377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks. I needed to change it around and use double rather than long. For some reason using long truncated the decimal part. I don't know if this is the best way but I did find something that seems to work. The problem is I don't know when Java will think it is a double or a long.
 
Stephan van Hulst
Saloon Keeper
Posts: 15526
364
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Ranch Hand
Posts: 377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks again. I guess I wasn't clear. Since I'm using JSON, curr is a JSONObject.
 
Stephan van Hulst
Saloon Keeper
Posts: 15526
364
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
From which library?
 
Dennis Putnam
Ranch Hand
Posts: 377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I didn't know there was more than 1. My IDE imported org.json.simple.
 
Stephan van Hulst
Saloon Keeper
Posts: 15526
364
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So what do you get when you print curr.toString()?
 
Dennis Putnam
Ranch Hand
Posts: 377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
71.3
 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.

Winston
 
Dennis Putnam
Ranch Hand
Posts: 377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Stephan van Hulst
Saloon Keeper
Posts: 15526
364
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
But what does the value represent?
 
Winston Gutkowski
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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;
  ((Number) jsonValue).doubleValue()

HIH

Winston
 
Dennis Putnam
Ranch Hand
Posts: 377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think I know what is going on now. If the decimal part of the number is 0, then the JSONObject will not contain the '.' and it will be recognized as a long, otherwise it is a double. I'm not worried about overflow as these numbers will never exceed a long. Thanks for your solution.
 
Winston Gutkowski
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Dennis Putnam wrote:I'm not worried about overflow as these numbers will never exceed a long.


That's not the point. A double can't hold an integer larger than 52/53 bits (not 64) exactly, so you could get collisions on very large numbers.

Winston
 
Dennis Putnam
Ranch Hand
Posts: 377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The number (long or double) will always be [0,1000).
 
reply
    Bookmark Topic Watch Topic
  • New Topic