Implement function to atoi
convert a string to an integer.
If No valid conversion could be performed, a zero value is returned.
If the correct value is out of the range of representable values, int_max(2147483647) or int_min (-21474 83648) is returned.
Example
"10" => 10"-1" => -1"123123123123123" => 2147483647"1.0" => 1
1 Public classSolution {2 /**3 * @param str:a string4 * @return an integer5 */6 Public intatoi (String str) {7 if(str = =NULL|| Str.length () = =0)8 return 0;9str =Str.trim ();Ten if(str.length () = =1&& (Str.equals ("+") || Str.equals ("-") || Str.equals (".") || Str.equals ("0"))) { One return 0; A } - -Boolean ispositive =true; the LongCurrent =0; - - for(inti =0; I < str.length (); i++) { - if(i = =0&& Str.charat (i) = ='+') { + Continue; -}Else if(i = =0&& Str.charat (i) = ='-') { +Ispositive =false; A}Else if((Str.charat (i) = ='.'&& i = = Str.length ()-1) at|| (Str.charat (i) = ='.'&& i = = Str.length ()-2&& Str.charat (Str.length ()-1) =='0')) { - Break; -}Else if(Str.charat (i) >='0'&& Str.charat (i) <='9') { -Current = current *Ten+ Str.charat (i)-'0'; - if(Ispositive && Current >integer.max_value) - returnInteger.max_value; in if(!ispositive &&-current <integer.min_value) - returnInteger.min_value; to}Else { + Break; - } the } * $ if(!ispositive) {Panax NotoginsengCurrent =-Current ; - } the + return(int) Current; A } the}
String to Integer