String converting to a number in Java
Forums:
//to int
String strNumber = "42";
//the first method
int number = Integer.parseInt(strNumber);
//the second method
number = Integer.valueOf(strNumber);
//to Double and Float
//the first method
String strPi = "3.14";
float pi = Float.parseFloat(strPi);
//the second method
String strExp = "2.71828";
double exp = Double.valueOf(strExp);
//the third method
String strHalf = "0,5";
DecimalFormat formatter = new DecimalFormat();
DecimalFormatSymbols sfs = new DecimalFormatSymbols();
sfs.setDecimalSeparator(',');
formatter.setDecimalFormatSymbols(sfs);
double half = 0;
try {
half = formatter.parse(strHalf).doubleValue();
} catch (ParseException e) {
e.printStackTrace();
}
- Log in to post comments