How can i convert a string to a abstract number, provided string is any valid number in java (say int, long, double etc).
I will not know the type of number in the string, so i can't use specific primitive parsing (like Integer.parseInt, Long.parseLong etc). Is there any generic way to convert it?
Eg:
The unary plus operator ( + ) will convert a string into a number. The operator will go before the operand. We can also use the unary plus operator ( + ) to convert a string into a floating point number. If the string value cannot be converted into a number then the result will be NaN .
We can convert String to an int in java using Integer.parseInt() method. To convert String into Integer, we can use Integer.valueOf() method which returns instance of Integer class.
Use NumberFormat. Number cannot be instantiated because it is an abstract class.
Number number = NumberFormat.getInstance().parse(string);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With