convert string to int without parseint

Convert string to integer without using build in api Integer.parseInt()

   Using this concept we can convert String to Number, String to Float, String to Double, String to short and long
                                                       
ASCII code of decimal number start form 48

  • public class ConvertStringToInt {

  • public static void main(String[] args) {
  • String str="-14545";
  • int total=0;
  • boolean m=false;
  • for(int i=0;i<str.length();i++){
  • if(str.charAt(i)=='-'){
  • m=true;
  • continue;
  • }
  • if(str.charAt(i)=='+'){
  • continue;
  • }
  • total=(total*10)+(str.charAt(i)-48);
  • }
  • if(m){
  • total=0-total;
  • }
  • System.out.println(total);


  • }

  • }



Comments

Popular posts from this blog

I get wrong characters when retreiving the message body of an email using TIdIMAP4.UIDRetrieveTextPeek2()

How to drop the all the 1's in a correlation matrix

Today Walkin 14th-Sept