Is there a Java class that provides a method to convert a string to the appropriate numeric data type (int, float, double) based on the contents of the string. (Whether the String contains a decimal point and the number of digits.)
I can write this logic from scratch, but want to use the "standard" way to do this, if there is one.
If you're using JDK 5+ I would recommend Scanner, using methods like hasNextInt() and nextInt(). The nice thing about Scanner is that if the input can't be pared as an int (or whatever numeric type you want) you can detect this easily beforehand using hasNextInt(). Prior to JDK 5 the standard option was to use Integer.parseInt(), but in that case you need to catch an exception in case of bad input.
And yes, I agree that NumberFormat / DecimalFormat are probably best if you need to customize the format options. In my experience that's been pretty rare, but it could happen.
"I'm not back." - Bill Harding, Twister
I’ve looked at a lot of different solutions, and in my humble opinion Aspose is the way to go. Here’s the link: http://aspose.com
subject: Converting a String to the appropriate numeric data type...