Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 06-29-2008, 01:32 PM
Member
 
Join Date: Jun 2008
Location: Australia
Posts: 19
javanewbie is on a distinguished road
Money to Words
Good day to everyone.

Right here, I do have here a program that allows numbers to convert them into words.

Code:
import java.io.*; class MoneyConversion { 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(8010)); 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)); } 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(); } } /* *** 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 */
I'm still finding a way where I can add up decimal values.

Example:

123.45 = one hundred twenty three and forty five cents.

Anyone knows how?


Thanks in advance.
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 06-29-2008, 10:05 PM
Nicholas Jordan's Avatar
Senior Member
 
Join Date: Jun 2008
Location: Southwest
Posts: 422
Nicholas Jordan is on a distinguished road
one idea
Code:
if( ( number - 10 ) > 0 ){StringBuffer.append();//} if( ( number - 100 ) > 0 ){StringBuffer.append();//} if( ( number - 1000 ) > 0 ){StringBuffer.append();//}
modulo operator does the opposite of what you want to do, use StringBuffer to build up what is written to System.out.

Just a short, once through. Other places depend on this being worked first.
__________________
Please provide your feedback on our
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
.
Cybercartography: A new theoretical construct proposed by D.R. Fraser Taylor
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 06-30-2008, 05:27 AM
Norm's Avatar
Senior Member
 
Join Date: Jun 2008
Location: SW MO, USA
Posts: 972
Norm is on a distinguished road
Convert the number to string. Then use the position of each character in the string to determine what value it is. Sort of like you do when you look at a number.
Split it at the decimal point
For example 123.45 1 in hundreds position, 2 in 10s and 3 in unit
then for the fraction. it has 2 positions, so 4 10s and 5 in unit
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Scrambling Words Shadow22202 New To Java 9 04-30-2008 04:51 AM
help w words Gilgamesh New To Java 5 11-21-2007 07:15 PM
program help: Extracting words from a string toad New To Java 1 11-04-2007 07:39 PM
Analyze a string of words zoe Advanced Java 2 07-26-2007 11:01 AM
Aspose.Words for Java - 2.1.0.0 levent Java Announcements 0 05-24-2007 11:08 AM


All times are GMT +3. The time now is 06:04 PM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org