Core Java| JSP| Servlets| XML| EJB| JEE5| Web Services| J2ME| Glossary| Questions?

 

 

 

 

 

 

 

 

 

 

 

 

 

Search Tutorials:
 

Software Solutions and Services
 

 
  JDO Tutorials
  EAI Articles
  Struts Tutorials
  Java Tutorials
  Java Certification
  Java Applet
Questions
Comments
 
Convert Number To Words 
 

In this example, We are going to convert number to words.

 

Convert Number To Words

                         

In this example, We are going to convert number to words.

Code Description:

The following program takes the two static array of Strings . Here, we are going to make a method in which a parameter of integer type is passed. After that this program is going to create an another method of name convert() . In the main class create an object, and pass the value in object (num) . It displays the string representing the number.

Here is the code of this program:

public class NumberToWords{
  static final String[] Number1 = {""," Hundrad"};
  static final String[] Number2 = {"","One","Two""Three","Four","Five",
    " Six"," Seven""Eight"," Nine","Ten" };
  String number(int number){
    String str;
    if (number % 100 10){
      str = Number2[number % 100];
      number /= 100;
    }
    else {
      str= Number2[number % 5];
      number /= 5;
    }
    if (number == 0return str;
    return Number2[number"hundred" + str;
  }
  public String convert(int number) {
    if (number == 0){
      return "zero"
    }
    String pre = "";
    String str1 = "";
    int i = 0;
    do {
      int n = number % 100;
      if (n != 0){
        String s = number(n);
        str1 = s + Number1[i+ str1;
      }
      i++;
      number /= 100;
    }
    while (number > 0);
    return (pre + str1).trim();
  }
  public static void main(String[] args) {
    NumberToWords num = new NumberToWords();
    System.out.println("words is :=" + num.convert(0));
    System.out.println("words is :=" + num.convert(1));
    System.out.println("words is :=" + num.convert(9));
    System.out.println("words is :=" + num.convert(100));
  }
}

Download of this program:

Output of this program.

C:\corejava>java NumberToWords
words is :=zero
words is :=One
words is :=Nine
words is :=One Hundrad
C:\corejava>

                         

» View all related tutorials
Related Tags: c convert io help this program to learn ram ear e il it section li in converting m ps all

Leave your comment:

Name:

Email:

URL:

Title:

Comments:


Enter Code:

Audio Version
Reload Image
 

Note: Emails will not be visible or used in any way, and are not required. Please keep comments relevant. Any content deemed inappropriate or offensive may be edited and/or deleted.

No HTML code is allowed. Line breaks will be converted automatically. URLs will be auto-linked. Please use BBCode to format your text.

Add This Tutorial To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 

Current Comments

13 comments so far (
post your own) View All Comments Latest 10 Comments:

I want program to convert number to word,word to number,binary to hex, hex to binary,binary to octal & octal to binary

Posted by poonam on Sunday, 02.1.09 @ 00:10am | #84367

This is a buggy code..I posted the correct code..which i don't think was approved and posted on this page..

Posted by Raj on Tuesday, 09.30.08 @ 02:28am | #80786

public class NumberToWords{
static final String[] Number1 = {""," Hundred"};
static final String[] Number2 = {"","One","Two", "Three","Four","Five", " Six"," Seven", "Eight"," Nine","Ten" };
static final String[] Number3 = {"", "Ten", "Twenty", "Thirty", "Fourty", "Fifty", "Sixty", "Seventy", "Eighty","Ninety"};
static final String[] Number4 = {"", "Eleven", "Twelve", "Thirteen", "Fourteen", "Fifteen", "Sixteen", "Seventeen", "Eighteen", "Nineteen"};

String number(int number){
String str;
if (number % 100 < 10){
str = Number2[number % 100];
number /= 100;
}
else{
if( (number % 10) == 0){
return Number3[number / 10];
}
else{
if((number % 10) < 10){
str = Number3[number / 10];
number /=10;
}else{
str = Number2[number % 10];
number /= 10;
}
if (number == 0) return str ;
return str + " " + Number2[number] ;
}
}
if (number == 0) return str;
return Number2[number] + "hundred" + str;
}

private String convert(int number) {

if(number < 0 || number > 999)
return "Invalid Number - Should be (0-999)";

if (number == 0){
return "zero";
}
String pre = "";
String str1 = "";
int i = 0;
if( (number > 10 && number < 19)){
return Number4[ number % 10];
}else{
do {
int n = number % 100;
if (n != 0){
String s = number(n);
str1 = s + Number1[i] + " " + str1;
}
i++;
number /= 100;
}
while (number > 0);
}

return (pre + str1).trim();
}
public static void main(String[] args) {
NumberToWords num = new NumberToWords();
System.out.println("words is :=" + num.convert(13));
System.out.println("words is :=" + num.convert(200));
System.out.println("words is :=" + num.convert(999));
System.out.println("words is :=" + num.convert(10));
System.out.println("words is :=" + num.convert(179));
System.out.println("words is :=" + num.convert(44));
System.out.println("words is :=" + num.convert(900));
System.out.println("words is :=" + num.convert(9221));
}
}

Posted by Raj on Thursday, 09.25.08 @ 02:51am | #80678

import java.util.HashMap;

public class NumberToWords{
static final String[] Number1 = {""," Hundred"};
static final String[] Number2 = {"","One","Two", "Three","Four","Five", " Six"," Seven", "Eight"," Nine","Ten" };

static HashMap<Integer,String> numHash = new HashMap<Integer,String> ();

String number(int number){
String str;
if (number % 100 < 10){
str = Number2[number % 100];
number /= 100;
}
else{

if( (number % 10) == 0){
return Number2[number / 10] + " zero";
}
else{
str = Number2[number % 10];
number /= 10;
if (number == 0) return str ;
return Number2[number] + " " + str;
}
}
if (number == 0) return str;
return Number2[number] + "hundred" + str;
}

private void setHash(){
numHash.put(11, "Eleven");
numHash.put(12, "Twelve");
numHash.put(13, "Thirteen");
numHash.put(14, "Fourteen");
numHash.put(15, "Fifteen");
numHash.put(16, "Sixteen");
numHash.put(17, "Seventeen");
numHash.put(18, "Eighteen");
numHash.put(19, "Nineteen");
}
private String convert(int number) {

if(number < 0 || number > 999)
return "Invalid Number - Should be (0-999)";

if (number == 0){
return "zero";
}
String pre = "";
String str1 = "";
int i = 0;
if( (number > 10 && number < 19)){
return numHash.get(number);
}else{
do {
int n = number % 100;
if (n != 0){
String s = number(n);
str1 = s + Number1[i] + str1;
}
i++;
number /= 100;
}
while (number > 0);
}

return (pre + str1).trim();
}
public static void main(String[] args) {
NumberToWords num = new NumberToWords();
num.setHash();
System.out.println("words is :=" + num.convert(10));
System.out.println("words is :=" + num.convert(200));
System.out.println("words is :=" + num.convert(999));
System.out.println("words is :=" + num.convert(20));
System.out.println("words is :=" + num.convert(179));
System.out.println("words is :=" + num.convert(44));
System.out.println("words is :=" + num.convert(-12));
System.out.println("words is :=" + num.convert(9221));
}
}

Posted by Raj on Thursday, 09.25.08 @ 01:03am | #80677

public class NumberToWords{
static final String[] Number1 = {""," Hundred"};
static final String[] Number2 = {"","One","Two", "Three","Four","Five",
" Six"," Seven", "Eight"," Nine","Ten" };
String number(int number){
String str;
if (number % 100 < 10){
str = Number2[number % 100];
number /= 100;
}
else{

if( (number % 10) == 0){
return Number2[number / 10] + " zero";
}
else{
str = Number2[number % 10];
number /= 10;
if (number == 0) return str ;
return Number2[number] + " " + str;
}

}

if (number == 0) return str;
return Number2[number] + "hundred" + str;
}
public String convert(int number) {
if (number == 0){
return "zero";
}
String pre = "";
String str1 = "";
int i = 0;
do {
int n = number % 100;
if (n != 0){
String s = number(n);
str1 = s + Number1[i] + str1;
}
i++;
number /= 100;
}
while (number > 0);
return (pre + str1).trim();
}
public static void main(String[] args) {
NumberToWords num = new NumberToWords();
System.out.println("words is :=" + num.convert(13));
System.out.println("words is :=" + num.convert(70));
System.out.println("words is :=" + num.convert(999));
System.out.println("words is :=" + num.convert(20));
}
}

Posted by Raj on Thursday, 09.25.08 @ 00:15am | #80675

In the above solution
else {
str= Number2[number % 5];
number /= 5;
}

should be replaced by

else {
str= Number2[number % 10];
number /= 10;
if (number == 0) return str;
return Number2[number] + str;
}

The program does not handle case for 20, 30, 40 etc..

Posted by Raj on Thursday, 09.25.08 @ 00:04am | #80674

that is great but the problem here is user's input is required, and it has to convert from 0 to 20,000. but our teacher say if we can do it up to 100,000 he will give whoever does that a high grade. i don't know why with the program that i did it has error, if i type 1015 it will show one thousand tenfive fifteen. ANd out teacher says we have to use SWITCH

Posted by kinderprogrammer on Monday, 09.15.08 @ 16:30pm | #79579

send me fast

Posted by prashant vyas on Saturday, 05.31.08 @ 11:11am | #61540

I Want The Code For Number To Word ConVersion Using JavaScript

Posted by sasikumar on Monday, 05.19.08 @ 14:50pm | #60444

hi,
how can convert the rupees into word.
Rupees could be more than million as well asd it may contain paise.

for example : 58234453434.50

with regards,
gopi

Posted by gopimaharajan on Friday, 03.7.08 @ 18:43pm | #51853

Training Courses
Tell A Friend
Your Friend Name
Website Designing Services
 
Web Designing Packages From $150!
 
Website Designing Company Web Hosting
 
Website Designing Quotation
 
Search Tutorials:

 

 
 

Home | JSP | EJB | JDBC | Java Servlets | WAP  | Free JSP Hosting  | Search Engine | News Archive | Jboss 3.0 tutorial | Free Linux CD's | Forum | Blogs

About Us | Advertising On RoseIndia.net  | Site Map

India News

Indian Software Development Company | iPhone Development Company in India | Flex Development Company in India | Java Training Delhi | Java Training at Noida |

Send your comments, Suggestions or Queries regarding this site at roseindia_net@yahoo.com.

Copyright © 2008. All rights reserved.