corejava

corejava

View Answers

August 26, 2008 at 10:19 AM

Hi friend,


import java.io.*;
class NumToWords {
private static final String[] maxWords = {
"",
" thousand",
" million",
" billion",
" trillion",
" quadrillion",
" quintillion"
};

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

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

private String oneThousand(int num) {
String str;

if (num % 100 < 20){
str = oneWords[num % 100];
num /= 100;
}
else {
str = oneWords[num % 10];
num /= 10;

str = tensWords[num % 10] + str;
num /= 10;
}
if (num == 0) return str;
return oneWords[num] + " hundred" + str;
}

public String convert(int num) {
/* special case */
if (num == 0) { return "zero"; }

String preStr = "";

if (num < 0) {
num = -num;
preStr = "negative";
}

String str = "";
int place = 0;

do {
int n = num % 1000;
if (n != 0){
String s = oneThousand(n);
str = s + maxWords[place] + str;
}
place++;
num /= 1000;
} while (num > 0);

return (preStr + str).trim();
}

public static void main(String[] args) throws IOException {

BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter the number");
String strNum = br.readLine();

int num = Integer.parseInt(strNum);
NumberToWords ntw = new NumberToWords();
System.out.println("Result is : " + ntw.convert(num));


}
}

For read for more information :

http://www.roseindia.net/java/java-conversion/convert-number-to-words.shtml

Thanks









Related Tutorials/Questions & Answers:
CoreJava Project
CoreJava Project  Hi Sir, I need a simple project(using core Java, Swings, JDBC) on core Java... If you have please send to my account
corejava
Advertisements
corejava
CoreJava
corejava - Java Beginners
corejava pass by value semantics  Example of pass by value semantics in Core Java.  Hi friend,Java passes parameters to methods using pass by value semantics. That is, a copy of the value of each of the specified
Corejava Interview,Corejava questions,Corejava Interview Questions,Corejava
;    Q 1. When should I use the abstract class rather... it by extending the abstract class.    Q 2. What is hashcode? When....    Q 3. How can I get a class reference without using new operator ? Ans
Corejava Interview,Corejava questions,Corejava Interview Questions,Corejava
;    Q 1. How can I get the full path of Explorer.exe... that system can be access from an applet loaded via the Internet.     Q 2....   Q 3. Is it possible to convert a string to an abstract path ? Ans
Corejava Interview,Corejava questions,Corejava Interview Questions,Corejava
;    Q 1 : How should I create an immutable class ? Ans...; Q 2 : Is println overloading or overriding ?  Ans : The println() method...;     Q 3 : What is dynamic method dispatch ?  Ans
Corejava Interview,Corejava questions,Corejava Interview Questions,Corejava
CoreJava - Java Beginners
corejava - Java Interview Questions
corejava - Java Interview Questions
corejava - Java Interview Questions
corejava - Java Interview Questions
corejava - Java Interview Questions
corejava - Java Interview Questions
corejava - Java Beginners
Corejava - Java Interview Questions
corejava - Java Beginners
corejava - Java Beginners
corejava - Java Beginners
corejava - Java Beginners
corejava - Java Beginners
corejava - Java Beginners
JList
JList  pls tell me about the concept the JList in corejava? and tell me a suitable example
core java - Java Beginners
core java  how to create a login page using only corejava(not servlets,jsp,hibernate,springs,structs)and that created loginpage contains database(ms-access) the database contains data what ever u r enter and automatically date
Java Locale
/corejava/javatext/parseAndFormatDateUsingLocale.html Thanks
Eclipse - IDE Questions
Eclipse  Hi To All.. I am new to ECLIPSE IDE. Please suggest me any book which can give me complete idea of setting environment and development with basic to advance development of java like(corejava,servlet,jsp,struts,ejb
What is Locale - Java Beginners
://www.roseindia.net/tutorial/java/corejava/javatext
HOW TO BECOME A GOOD PROGRAMMER
HOW TO BECOME A GOOD PROGRAMMER  I want to know how to become good programmer   Hi Friend, Please go through the following link: CoreJava Tutorials Here you will get lot of examples with illustration where you can
autentication & authorisation - JSP-Servlet
/interviewquestions/corejava/null-marker-interfaces-in-java.shtml Thanks
how to count words in string using java
how to count words in string using java  how to count words in string using corejava   Hi Friend, Try the following code: import java.util.*; class CountWords{ public static void main(String[] args
Inter Thread Communication
://www.devmanuals.com/tutorials/java/corejava/InterthreadCommunication.html
Factorial Program in Java
"); } } } Output C:\Documents and Settings\bharat\Desktop\bipul\CoreJava...:\Documents and Settings\bharat\Desktop\bipul\CoreJava\Swing Example\Factorial>
using oops concepts - Development process
total source code in corejava using oops concepts.  Hi
What are the prerequisites for learning Java?
for practicing.   Corejava Interview,Corejava questions,Corejava Interview Questions,Corejava  - In this section we have interview questions... Glossary Q - Java Terms R - Java Terms S - Java Terms T
Interthread Communication in Java
corejava; class Queue { int n; boolean valueSet = false; synchronized...(); } } class Producer implements Runnable { Queue q; Producer(Queue q) { this.q = q; new Thread(this, "Producer").start(); } public
What are the prerequisites for learning Java?
Tutorials - Many example of Java/JDK for practicing.   Corejava Interview,Corejava questions,Corejava Interview Questions,Corejava  ... Glossary O - Java Glossary P - Java Glossary Q - Java Terms
JAXB Create XML File And Get Data From XML
and Settings\\bharat\\Desktop\\bipul\\CoreJava\\employee.xml"); JAXBContext... File("C:\\Documents and Settings\\bharat\\Desktop\\bipul\\CoreJava
help me in these - Java Interview Questions
use the corejava.*  Hi Friend, 1) import java.util.*; public class
Mutliple files upload
; if (skilltype == "Java") { list = new Array("CoreJava","J2EE","J2SE..."); listValue = new Array("CoreJava","J2EE","J2SE","Struts","Hibernet","Springs","JSP
Mutliple files upload
; if (skilltype == "Java") { list = new Array("CoreJava","J2EE","J2SE..."); listValue = new Array("CoreJava","J2EE","J2SE","Struts","Hibernet","Springs","JSP
Mutliple files upload
; if (skilltype == "Java") { list = new Array("CoreJava","J2EE","J2SE..."); listValue = new Array("CoreJava","J2EE","J2SE","Struts","Hibernet","Springs","JSP
Mutliple files upload
== "Java") { list = new Array("CoreJava","J2EE","J2SE..."); listValue = new Array("CoreJava","J2EE","J2SE","Struts","Hibernet","Springs","JSP
Mutliple files upload
; if (skilltype == "Java") { list = new Array("CoreJava","J2EE","J2SE..."); listValue = new Array("CoreJava","J2EE","J2SE","Struts","Hibernet","Springs","JSP
JSF Basic Interview Questions
strong basic knowledge of corejava and related web technologies. For your

Ads