Community discussion forum

java

Tags: java, vb6 India
  • 4 years ago

    i want 2 convert numbers 2 words in java ie '122' to 'one hundred and twenty two'

  • 4 years ago

    krish u have come 2 the wrong forum

  • 4 years ago

    im sorry roshanteg

  • 4 years ago
    Hi,

    If u Can go to sunmicrosystems web page they have their own java forums; hope that will help u

    Good Luck,
  • 4 years ago
    ok yaar
  • 4 years ago
    this is a code that help you to display your numbers into text format (any language).

    class EnglishDecimalFormat {
     private static final String[] majorNames = {
       "",
       " thousand",
       " million",
       " billion",
       " trillion",
       " quadrillion",
       " quintillion"
       };

     private static final String[] tensNames = {
       "",
       " ten",
       " twenty",
       " thirty",
       " fourty",
       " 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 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 String convert(int number) {
       /* special case */
       if (number == 0) { return "zero"; }

       String prefix = "";

       if (number < 0) {
           number = -number;
           prefix = "negative";
         }

       String soFar = "";
       int place = 0;

       do {
         int n = number % 1000;
         if (n != 0){
            String s = convertLessThanOneThousand(n);
            soFar = s + majorNames[place] + soFar;
           }
         place++;
         number /= 1000;
         } while (number > 0);

       return (prefix + soFar).trim();
    }

    public static void main(String[] args) {
       EnglishDecimalFormat f = new EnglishDecimalFormat();
       System.out.println("*** " + f.convert(0));
       System.out.println("*** " + f.convert(1));
       System.out.println("*** " + f.convert(16));
       System.out.println("*** " + f.convert(100));
       System.out.println("*** " + f.convert(118));
       System.out.println("*** " + f.convert(200));
       System.out.println("*** " + f.convert(219));
       System.out.println("*** " + f.convert(800));
       System.out.println("*** " + f.convert(801));
       System.out.println("*** " + f.convert(1316));
       System.out.println("*** " + f.convert(1000000));
       System.out.println("*** " + f.convert(2000000));
       System.out.println("*** " + f.convert(3000200));
       System.out.println("*** " + f.convert(700000));
       System.out.println("*** " + f.convert(9000000));
       System.out.println("*** " + f.convert(123456789));
       System.out.println("*** " + f.convert(-45));
       /*
       *** zero
       *** one
       *** sixteen
       *** one hundred
       *** one hundred eighteen
       *** two hundred
       *** two hundred nineteen
       *** eight hundred
       *** eight hundred one
       *** one thousand three hundred sixteen
       *** one million
       *** two million
       *** three million two hundred
       *** seven hundred thousand
       *** nine million
       *** one hundred twenty three million four hundred fifty six thousand seven hundred eighty nine
       *** negative fourty five
       */
       }
    }


    enjoy it!
  • 4 years ago
    You can handle "dollar and cent" conversion by calling the "convert" method two times.

    like this:

    String phrase = "12345.67" ;
    Float num = new Float( phrase ) ;
    int dollars = (int)Math.floor( num ) ;
    int cent = (int)Math.floor( ( num - dollars ) * 100.0f ) ;

    String s = "$ " + f.convert( dollars ) + " and " + f.convert( cent ) + " cents" ;

    java was beated!
  • 4 years ago
    hi DoctorMahdi,
    the code which u gave was reallly sounded.
    but can u give a code without out using arrays.
    thanking u.

Post a reply

Enter your message below

Sign in or Join us (it's free).

We'd love to hear what you think! Submit ideas or give us feedback