Numbers to text - OR help with ASP or DELPHI to JAVA

If you know delphi or ASP you might wanna check out the links in the bottom and help me translate it to Java...

I'd like help with a program that prints text instead of numbers eg.

123 = onehundredtwentythree

I have som ideas, but they seem way complex for my understanding and seems like more code than nessecary...

Thank you for any help and sorry for any language errors, I'm from Sweden

DELPHI:

http://dn.codegear.com/article/23195

ASP:

http://forums.aspfree.com/visual-basic-programming-38/converting-numbers-to-text-help-please-43310.html

[602 byte] By [gothxxa] at [2007-11-27 5:09:11]
# 1
92 |--> quatre-vingt-douze
Hippolytea at 2007-7-12 10:28:48 > top of Java-index,Java Essentials,New To Java...
# 2

public class NumberToWords

{

static final String[] units = { "", "one", "two", "three", "four",

"five", "six", "seven", "eight", "nine" };

static final String[] teens = { "ten", "eleven", "twelve", "thirteen",

"fourteen", "fifteen", "sixteen",

"seventeen", "eighteen", "nineteen" };

static final String[] tens = { "twenty", "thirty", "forty", "fifty",

"sixty", "seventy", "eighty", "ninety" };

static final String[] mags = { "", "thousand", "million", "billion",

"trillion", "quadrillion", "quintillion" };

public static String numToWords(int num)

{

if (num == 0)

{

return "zero";

}

StringBuilder sb = new StringBuilder();

if (num < 0)

{

sb.append("minus");

}

String numStr = String.valueOf(num).replaceFirst("^-", "");

String[] parts = numStr.split("(?=(?:\\d\\d\\d)++$)");

int mag = parts.length - 1;

for (int i = 0; i <= mag; i++)

{

if (parsePart(parts[i], sb) && mag > 0 && i < mag)

{

sb.append(" ").append(mags[mag - i]).append(",");

}

}

return sb.toString();

}

static boolean parsePart(String str, StringBuilder sb)

{

boolean notZeroes = false;

int pos = 0;

char ch = (char)0;

if (str.length() == 3 && (ch = str.charAt(pos++)) != '0')

{

spaceIfNeeded(sb);

sb.append(units[ch - '0']).append(" hundred");

notZeroes = true;

}

if (str.length() > 1 && (ch = str.charAt(pos++)) != '0')

{

spaceIfNeeded(sb);

notZeroes = true;

if (ch == '1')

{

sb.append(teens[str.charAt(pos)]);

return notZeroes;

}

sb.append(tens[ch - '2']);

}

if (str.length() > 0 && (ch = str.charAt(pos)) != '0')

{

spaceIfNeeded(sb);

sb.append(units[ch - '0']);

notZeroes = true;

}

return notZeroes;

}

static void spaceIfNeeded(StringBuilder sb)

{

if (sb.length() > 0)

{

sb.append(" ");

}

}

public static void main(String... args)

{

int[] test = new int[] { 1, 23, 456, 7890, -1234567890 };

for (int n : test)

{

System.out.printf("%n%,d%n", n);

System.out.printf("%s%n", numToWords(n));

}

}

}

uncle_alicea at 2007-7-12 10:28:48 > top of Java-index,Java Essentials,New To Java...