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.

10
  • 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?) Commented Oct 8, 2015 at 14:11
  • In general, you can't. Method overloading does not allow you to mix return types. Commented Oct 8, 2015 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 Commented Oct 8, 2015 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. Commented Oct 8, 2015 at 14:25
  • True. It doesn't really matter but I'd want it to return the most suitable subclass. Commented Oct 8, 2015 at 14:29

3 Answers 3

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

Seems to do the trick...

Sign up to request clarification or add additional context in comments.

8 Comments

Only returns Long and Double. Not quite what you asked for originally.
I don't see that in the documentation, but it is sufficient for my needs. And yes, it is "quite" what I asked for.
Here's the link to the JavaDoc. docs.oracle.com/javase/8/docs/api/java/text/… (Notice that the details for parse(String) refer one to the details for parse(String, ParsePosition) for additional information.
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".
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.)
|
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();

Comments

1

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.