5

How do I parse ANY Number type from a String in Java?

I'm aware of the methods like Integer.parseInt(myString) and Double.parseDouble(myString) but what I optimally want is a method like Number.parseNumber(myString) which doesn't exist. How can I achieve that behaviour in another way? (I want the parsed Number to reflect the String "exactly" in terms of for example number of decimals).

Example:

"0" => Number (internally of Integer subclass)

"0.0" => Number (internally of Double subclass)

Also, no ugliness like checking for decimal separators etc.

Joakim
  • 3,224
  • 3
  • 29
  • 53
  • `new BigDecimal(yourString)`. That should give you a `Number` representing the value of the string. (If you say *but I want it as a `double` if it's "0.0" and as an `int` if it's "0"*, I don't really understand how you would expect it to work. What type would the variable have, that you want to store the result in?) – aioobe Oct 08 '15 at 14:11
  • In general, you can't. Method overloading does not allow you to mix return types. – Phylogenesis Oct 08 '15 at 14:13
  • Just to clarify... Float, Double, Integer Long etc. all extend the Number superclass, which is abstract but I can still have a variable of that type. I expect it to return a Number that is internally any of those classes – Joakim Oct 08 '15 at 14:15
  • Do you want it to use the smallest type that fits? E.g. "1024" fits `Short`, `Integer` _and_ `Long`. Ideally, I'm guessing you'd want it to return `Short`. – Erick G. Hagstrom Oct 08 '15 at 14:25
  • True. It doesn't really matter but I'd want it to return the most suitable subclass. – Joakim Oct 08 '15 at 14:29
  • Duplicate of https://stackoverflow.com/questions/1163615/java-library-to-check-whether-a-string-contains-a-number-without-exceptions/1166372#1166372 – Jool Oct 08 '15 at 14:29
  • @Jool not a dup. That one is only for checking _whether_ a string is a number, not about getting the number out of the string. – Erick G. Hagstrom Oct 08 '15 at 14:30
  • @Erik - It does actually talk about parsing and getting the number, it's close enough. – Jool Oct 08 '15 at 17:07
  • What do you want this for? Java pre-allocates memory so variable creation is super fast. You can really just go ahead and turn it into a double and reduce precision as needed. – Meeesh Oct 08 '15 at 17:29
  • In my case, the numbers that are parsed are serialized into JSON and sent to a server. This makes sure that the number arrive to the server just as the user entered it – Joakim Oct 09 '15 at 06:43

3 Answers3

8
Number number = NumberFormat.getInstance().parse(myString);

Seems to do the trick...

Joakim
  • 3,224
  • 3
  • 29
  • 53
  • 4
    Only returns Long and Double. Not quite what you asked for originally. – Erick G. Hagstrom Oct 08 '15 at 14:22
  • I don't see that in the documentation, but it is sufficient for my needs. And yes, it is "quite" what I asked for. – Joakim Oct 08 '15 at 14:26
  • It's mentioned [here](http://docs.oracle.com/javase/7/docs/api/java/text/NumberFormat.html#parse(java.lang.String,%20java.text.ParsePosition)). – Phylogenesis Oct 08 '15 at 14:27
  • 1
    Here's the link to the JavaDoc. http://docs.oracle.com/javase/8/docs/api/java/text/NumberFormat.html#parse-java.lang.String-java.text.ParsePosition- (Notice that the details for `parse(String)` refer one to the details for `parse(String, ParsePosition)` for additional information. – Erick G. Hagstrom Oct 08 '15 at 14:28
  • 1
    And I misunderstood. I took your question to be about _any_ subtype of Number, including `BigInteger` and `BigDecimal`, since you require " the parsed Number to reflect the String exactly in terms of for example number of decimals". – Erick G. Hagstrom Oct 08 '15 at 14:29
  • 2
    And even if this is what *you* wanted, it's worth documenting for future readers that this may not be what they're looking for (in that they may want a factory method that returns more than just `Long` and `Double`.) – Phylogenesis Oct 08 '15 at 14:30
  • Agreed. If anyone posts such an answer I will make it the accepted one. – Joakim Oct 08 '15 at 14:32
  • Unfortunately, this is dependent on Locale. The following will always fail: `Locale.setDefault(new Locale("no", "NO")); NumberFormat.getInstance().parse(Integer.valueOf(-1).toString());`. For some reason, all Nordic countries use another unicode character for minus! – Johannes Brodwall May 12 '22 at 09:15
2

Better way is to use BigDecimal class.

BigDecimal n = new BigDecimal("1.0");

Methods for needed values

n.byteValue();  
n.intValue();  
n.shortValue();  
n.longValue();  
n.floatValue();  
n.doubleValue();
Zé Henriques
  • 323
  • 1
  • 3
  • 15
0

You are probably looking for BigDecimal.

Please remember that a number can be represented in many forms as a string. e.g. 1 is the same number as 1.0000000.

OldCurmudgeon
  • 64,482
  • 16
  • 119
  • 213