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.
new BigDecimal(yourString). That should give you aNumberrepresenting the value of the string. (If you say but I want it as adoubleif it's "0.0" and as anintif 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?)Short,IntegerandLong. Ideally, I'm guessing you'd want it to returnShort.