Tuesday, October 11, 2011

Convert Number to String

Convert Number to String in Java using Swing..... (Editor use: Net Bean)





firstly create new project. in this project add one Java class file and name this file "EnglishNumberToWords.Java"
 and copy following line of code in this file.....

*************************Code********************************



package number;
import java.text.DecimalFormat;

public class EnglishNumberToWords {

  private static final String[] tensNames = {
    "",
    " ten",
    " twenty",
    " thirty",
    " forty",
    " fifty",
    " sixty",
    " seventy",
    " eighty",
    " ninety"
  };

  private static final String[] numNames = {
    "",
    " one",
    " two",
    " three",
    " four",
    " five",
    " six",
    " seven",
    " eight",
    " nine",
    " ten",
    " eleven",
    " twelve",
    " thirteen",
    " fourteen",
    " fifteen",
    " sixteen",
    " seventeen",
    " eighteen",
    " nineteen"
  };

  private static String convertLessThanOneThousand(int number) {
    String soFar;

    if (number % 100 < 20){
      soFar = numNames[number % 100];
      number /= 100;
    }
    else {
      soFar = numNames[number % 10];
      number /= 10;

      soFar = tensNames[number % 10] + soFar;
      number /= 10;
    }
    if (number == 0) return soFar;
    return numNames[number] + " hundred" + soFar;
  }


  public static String convert(long number) {
    // 0 to 999 999 999 999
    if (number == 0) { return "zero"; }

    String snumber = Long.toString(number);
   String snumber1 = Long.toString(number);
    // pad with "0"
    String mask = "000000000000";
    String mask1 =  "000000000";
    DecimalFormat df = new DecimalFormat(mask);
    DecimalFormat df1 = new DecimalFormat(mask1);

    snumber = df.format(number);
    snumber1 = df1.format(number);
  
    // XXXnnnnnnnnn
    int billions = Integer.parseInt(snumber.substring(0,3));
    int coror = Integer.parseInt(snumber1.substring(0, 2));

    // nnnXXXnnnnnn
    int millions  = Integer.parseInt(snumber.substring(3,6));
    int lakh = Integer.parseInt(snumber1.substring(2,4));
    int thousand = Integer.parseInt(snumber1.substring(4,6));
    int hundred = Integer.parseInt(snumber1.substring(6,9));
    //System.out.println("---1---"+coror+"-------2--------"+lakh+"-----3--------"+thousand+"-------4-------"+hundred);
    String corors;
    switch(coror){
         case 0:
             corors = "";
             break;
        case 1:
            corors = convertLessThanOneThousand(coror)+" corror";
            break;
         default:
            corors = convertLessThanOneThousand(coror)+" corror";
      
    }
    String result1 =  corors;
    String lakhs;
    switch(lakh){
        case 0 :
            lakhs = "";
            break;
        case 1  :
            lakhs = convertLessThanOneThousand(lakh)+" lakh";
            break;
            default:
                lakhs = convertLessThanOneThousand(lakh)+" lakh";
              
    }
    result1 = result1+lakhs;
    String thou;
    switch(thousand){
        case 0 :
            thou = "";
            break;
        case 1:
            thou =lakhs = convertLessThanOneThousand(thousand)+" thausands";
            break;
            default:
                thou =lakhs = convertLessThanOneThousand(thousand)+" thausands";
    }
    result1 = result1+thou;
    result1 = result1+convertLessThanOneThousand(hundred);

    // nnnnnnXXXnnn
    int hundredThousands = Integer.parseInt(snumber.substring(6,9));
    // nnnnnnnnnXXX
    int thousands = Integer.parseInt(snumber.substring(9,12));  

    String tradBillions;
    switch (billions) {
    case 0:
      tradBillions = "";
      break;
    case 1 :
      tradBillions = convertLessThanOneThousand(billions)
      + " billlions ";
      break;
    default :
      tradBillions = convertLessThanOneThousand(billions)
      + " Billions ";
    }
    String result =  tradBillions;

    String tradMillions;
    switch (millions) {
    case 0:
      tradMillions = "";
      break;
    case 1 :
      tradMillions = convertLessThanOneThousand(millions)
      + " million ";
      break;
    default :
      tradMillions = convertLessThanOneThousand(millions)
      + " million ";
    }
    result =  result + tradMillions;

    String tradHundredThousands;
    switch (hundredThousands) {
    case 0:
      tradHundredThousands = "";
      break;
    case 1 :
      tradHundredThousands = "one thousand ";
      break;
    default :
      tradHundredThousands = convertLessThanOneThousand(hundredThousands)
      + " thousand ";
    }
    result =  result + tradHundredThousands;

    String tradThousand;
    tradThousand = convertLessThanOneThousand(thousands);
    result =  result + tradThousand;

    // remove extra spaces!
    return result1.replaceAll("^\\s+", "").replaceAll("\\b\\s{2,}\\b", " ");
  }

  /**
   * testing
   * @param args
   */
  public static void main(String[] args) {
      System.out.println("my one"+EnglishNumberToWords.convert(237898555));
    System.out.println("*** " + EnglishNumberToWords.convert(0));
    System.out.println("*** " + EnglishNumberToWords.convert(1));
    System.out.println("*** " + EnglishNumberToWords.convert(16));
    System.out.println("*** " + EnglishNumberToWords.convert(100));
    System.out.println("*** " + EnglishNumberToWords.convert(118));
    System.out.println("*** " + EnglishNumberToWords.convert(200));
    System.out.println("***219 " + EnglishNumberToWords.convert(219));
    System.out.println("*** " + EnglishNumberToWords.convert(800));
    System.out.println("*** " + EnglishNumberToWords.convert(801));
    System.out.println("*** " + EnglishNumberToWords.convert(1316));
    System.out.println("*** " + EnglishNumberToWords.convert(1000000));
    System.out.println("*** " + EnglishNumberToWords.convert(2000000));
    System.out.println("*** " + EnglishNumberToWords.convert(3000200));
    System.out.println("*** " + EnglishNumberToWords.convert(700000));
    System.out.println("*** " + EnglishNumberToWords.convert(9000000));
    System.out.println("*** " + EnglishNumberToWords.convert(9001000));
    System.out.println("*** " + EnglishNumberToWords.convert(123456789));
    System.out.println("*** " + EnglishNumberToWords.convert(2147483647));
    System.out.println("*** " + EnglishNumberToWords.convert(3000000010L));
}
}


****************************************************************


after that create one Java Frame with one Text Box, one Button and labels, design same like as above image

after that at Button Click Event (ActionPerformed event) add following code.....


*************************Code***********************************

 int Number = Integer.parseInt(txtNumber.getText());
        String No2String = EnglishNumberToWords.convert(Number)+"";
        txtString.setText(No2String);

in this code assign text box value to a "Number" variable and pass this value to the Function in above code "EnglishNumberToWords.Convert(Number)" and after that print the value

***************************Thanks************************************
Note : this project is available at  http://www.planet-source-code.com site

0 comments:

Post a Comment