How to parse negative number strings?
strings may be of the form:
-123.23
(123.23)
123.23-
is there a class that will convert any of the above strings to a number?
if not, what is the best way to do it?
Building on WarrenFaiths answer you can add this:
Double newNumber = 0;
if(number.charAt(i).isDigit()){
//parse numeber here using WarrenFaiths method and place the int or float or double
newNumber = Double.valueOf(number);
}else{
Boolean negative = false;
if(number.startsWith("-") || number.endsWith("-")){
number = number.replace("-", "");
negative = true;
}
if(number.startsWith("(") || number.endsWith(")"){
number = number.replace("(", "");
number = number.replace(")", "");
}
//parse numeber here using WarrenFaiths method and place the float or double
newNumber = Double.valueOf(number);
if(negative){
newNumber = newNumber * -1;
}
}
Im sorry if the replace methods are wrong, please correct me.