Home Java Java-conversion Convert Number To Words
Questions:Ask|Latest


 
 

Share on Google+Share on Google+

Convert Number To Words

Advertisement
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>
Advertisements

Liked it!  Share this Tutorial


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: July 10, 2007

Ask Questions?    Discuss: Convert Number To Words   View All Comments

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments
madhusudhan8
December 9, 2011
java

this is not work if number is in laks........ ex 150000 1 lack 50 thousund
dharmesh s vanza
January 20, 2012
number to words

automatic number to words
Pranjal
January 25, 2012
calling..

sir, how would i call this void function on jButton1. please tell me d coding for that..
vigneh
February 4, 2012
NumberToWords

can any pplease explain the NumberToWords program its difficult to understand
Pravin Bhaiswar
February 6, 2012
Problem in converting amount (Rupees) in word

Dear, I don't know vba but want to know how to convert amount into word. on the net, there is many way to do it. but when i copy it to another excel file, it does not work. So please tell me in detail how to do it? If possible, Send me excel sheet to my mail id i.e. bhaiswarpravin@gmail.com
shatrughan
February 29, 2012
thanks

thanks
viruz
November 22, 2013
Convert money with decimal places To Words

any code to convert money with decimal places?..
DMCA.com