Double value obtained from the transfer string type or int
Package com.shopping.test; public class Test { / ** * parsing str, obtained as integers * @param STR be resolved STR * / Private static int getIntValue (String STR) { int R & lt = 0 ; IF (STR! = null && str.length () = 0! ) { the StringBuffer BF = new new the StringBuffer (); char [] chars = str.toCharArray (); for ( int I = 0; I <chars.length; I ++ ) { char C = chars[i]; if (c >= '0' && c <= '9') { bf.append(c); } else if (c == ',') { continue; } else { if (bf.length() != 0) { break; } } } try { r = Integer.parseInt(bf.toString()); } catch (Exception e) { } } return R & lt; } / ** * parse the string to get double value, * @param STR * @return * / Private static Double getDoubleValue (String STR) { Double D = 0 ; IF ! (STR = null && str.length ! () = 0 ) { the StringBuffer BF = new new the StringBuffer (); char [] chars = str.toCharArray (); for ( int I = 0; I <chars.length; I ++ ) { char C = chars[i]; if (c >= '0' && c <= '9') { bf.append(c); } else if (c == '.') { if (bf.length() == 0) { continue; } else if (bf.indexOf(".") != -1) { break; } else { bf.append(c); } } else { if(bf.length () = 0! ) { BREAK ; } } } the try { D = Double.parseDouble (bf.toString ()); } the catch (Exception E) { } } return D; } public static void main (String [] args) { // for 1.5 (nominal values) String str1 = "for 1.5 (nominal values)" ; // String str1 = "0.730000019073486"; Double str1D = getDoubleValue (str1); System.out.println (str1+"->"+str1D); int i = getIntValue(str1); System.out.println(str1+"->"+i); } }
Note: This method can only get a value.