math - How to convert number to words in java -
we have crude mechanism convert numbers words (e.g. using few static arrays) , based on size of number translating english text. running issues numbers huge.
10183 = ten thousand 1 hundred eighty 3 90 = ninety 5888 = 5 thousand 8 hundred eighty 8
is there easy use function in of math libraries can use purpose?
here code, don't think there method in se.
it converts number string , parses string , associates weight
for example
1000
1
treated thousand position , 1
gets mapped "one"
, thousand because of position
code website:
english
import java.text.decimalformat; public class englishnumbertowords { private static final string[] tensnames = { "", " ten", " twenty", " thirty", " forty", " 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 englishnumbertowords() {} private static 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 static string convert(long number) { // 0 999 999 999 999 if (number == 0) { return "zero"; } string snumber = long.tostring(number); // pad "0" string mask = "000000000000"; decimalformat df = new decimalformat(mask); snumber = df.format(number); // xxxnnnnnnnnn int billions = integer.parseint(snumber.substring(0,3)); // nnnxxxnnnnnn int millions = integer.parseint(snumber.substring(3,6)); // nnnnnnxxxnnn int hundredthousands = integer.parseint(snumber.substring(6,9)); // nnnnnnnnnxxx int thousands = integer.parseint(snumber.substring(9,12)); string tradbillions; switch (billions) { case 0: tradbillions = ""; break; case 1 : tradbillions = convertlessthanonethousand(billions) + " billion "; break; default : tradbillions = convertlessthanonethousand(billions) + " billion "; } string result = tradbillions; string tradmillions; switch (millions) { case 0: tradmillions = ""; break; case 1 : tradmillions = convertlessthanonethousand(millions) + " million "; break; default : tradmillions = convertlessthanonethousand(millions) + " million "; } result = result + tradmillions; string tradhundredthousands; switch (hundredthousands) { case 0: tradhundredthousands = ""; break; case 1 : tradhundredthousands = "one thousand "; break; default : tradhundredthousands = convertlessthanonethousand(hundredthousands) + " thousand "; } result = result + tradhundredthousands; string tradthousand; tradthousand = convertlessthanonethousand(thousands); result = result + tradthousand; // remove spaces! return result.replaceall("^\\s+", "").replaceall("\\b\\s{2,}\\b", " "); } /** * testing * @param args */ public static void main(string[] args) { system.out.println("*** " + englishnumbertowords.convert(0)); system.out.println("*** " + englishnumbertowords.convert(1)); system.out.println("*** " + englishnumbertowords.convert(16)); system.out.println("*** " + englishnumbertowords.convert(100)); system.out.println("*** " + englishnumbertowords.convert(118)); system.out.println("*** " + englishnumbertowords.convert(200)); system.out.println("*** " + englishnumbertowords.convert(219)); system.out.println("*** " + englishnumbertowords.convert(800)); system.out.println("*** " + englishnumbertowords.convert(801)); system.out.println("*** " + englishnumbertowords.convert(1316)); system.out.println("*** " + englishnumbertowords.convert(1000000)); system.out.println("*** " + englishnumbertowords.convert(2000000)); system.out.println("*** " + englishnumbertowords.convert(3000200)); system.out.println("*** " + englishnumbertowords.convert(700000)); system.out.println("*** " + englishnumbertowords.convert(9000000)); system.out.println("*** " + englishnumbertowords.convert(9001000)); system.out.println("*** " + englishnumbertowords.convert(123456789)); system.out.println("*** " + englishnumbertowords.convert(2147483647)); system.out.println("*** " + englishnumbertowords.convert(3000000010l)); /* *** 0 *** 1 *** sixteen *** 1 hundred *** 1 hundred eighteen *** 2 hundred *** 2 hundred nineteen *** 8 hundred *** 8 hundred 1 *** 1 thousand 3 hundred sixteen *** 1 million *** 2 millions *** 3 millions 2 hundred *** 7 hundred thousand *** 9 millions *** 9 millions 1 thousand *** 1 hundred twenty 3 millions 4 hundred ** fifty 6 thousand 7 hundred eighty 9 *** 2 billion 1 hundred forty 7 millions ** 4 hundred eighty 3 thousand 6 hundred forty 7 *** 3 billion ten **/ } }
français quite different english version french lot more difficult!
package com.rgagnon.howto; import java.text.*; class frenchnumbertowords { private static final string[] dizainenames = { "", "", "vingt", "trente", "quarante", "cinquante", "soixante", "soixante", "quatre-vingt", "quatre-vingt" }; private static final string[] unitenames1 = { "", "un", "deux", "trois", "quatre", "cinq", "six", "sept", "huit", "neuf", "dix", "onze", "douze", "treize", "quatorze", "quinze", "seize", "dix-sept", "dix-huit", "dix-neuf" }; private static final string[] unitenames2 = { "", "", "deux", "trois", "quatre", "cinq", "six", "sept", "huit", "neuf", "dix" }; private frenchnumbertowords() {} private static string convertzerotohundred(int number) { int ladizaine = number / 10; int lunite = number % 10; string resultat = ""; switch (ladizaine) { case 1 : case 7 : case 9 : lunite = lunite + 10; break; default: } // séparateur "-" "et" "" string laliaison = ""; if (ladizaine > 1) { laliaison = "-"; } // cas particuliers switch (lunite) { case 0: laliaison = ""; break; case 1 : if (ladizaine == 8) { laliaison = "-"; } else { laliaison = " et "; } break; case 11 : if (ladizaine==7) { laliaison = " et "; } break; default: } // dizaines en lettres switch (ladizaine) { case 0: resultat = unitenames1[lunite]; break; case 8 : if (lunite == 0) { resultat = dizainenames[ladizaine]; } else { resultat = dizainenames[ladizaine] + laliaison + unitenames1[lunite]; } break; default : resultat = dizainenames[ladizaine] + laliaison + unitenames1[lunite]; } return resultat; } private static string convertlessthanonethousand(int number) { int lescentaines = number / 100; int lereste = number % 100; string sreste = convertzerotohundred(lereste); string resultat; switch (lescentaines) { case 0: resultat = sreste; break; case 1 : if (lereste > 0) { resultat = "cent " + sreste; } else { resultat = "cent"; } break; default : if (lereste > 0) { resultat = unitenames2[lescentaines] + " cent " + sreste; } else { resultat = unitenames2[lescentaines] + " cents"; } } return resultat; } public static string convert(long number) { // 0 à 999 999 999 999 if (number == 0) { return "zéro"; } string snumber = long.tostring(number); // pad des "0" string mask = "000000000000"; decimalformat df = new decimalformat(mask); snumber = df.format(number); // xxxnnnnnnnnn int lesmilliards = integer.parseint(snumber.substring(0,3)); // nnnxxxnnnnnn int lesmillions = integer.parseint(snumber.substring(3,6)); // nnnnnnxxxnnn int lescentmille = integer.parseint(snumber.substring(6,9)); // nnnnnnnnnxxx int lesmille = integer.parseint(snumber.substring(9,12)); string tradmilliards; switch (lesmilliards) { case 0: tradmilliards = ""; break; case 1 : tradmilliards = convertlessthanonethousand(lesmilliards) + " milliard "; break; default : tradmilliards = convertlessthanonethousand(lesmilliards) + " milliards "; } string resultat = tradmilliards; string tradmillions; switch (lesmillions) { case 0: tradmillions = ""; break; case 1 : tradmillions = convertlessthanonethousand(lesmillions) + " million "; break; default : tradmillions = convertlessthanonethousand(lesmillions) + " millions "; } resultat = resultat + tradmillions; string tradcentmille; switch (lescentmille) { case 0: tradcentmille = ""; break; case 1 : tradcentmille = "mille "; break; default : tradcentmille = convertlessthanonethousand(lescentmille) + " mille "; } resultat = resultat + tradcentmille; string tradmille; tradmille = convertlessthanonethousand(lesmille); resultat = resultat + tradmille; return resultat; } public static void main(string[] args) { system.out.println("*** " + frenchnumbertowords.convert(0)); system.out.println("*** " + frenchnumbertowords.convert(9)); system.out.println("*** " + frenchnumbertowords.convert(19)); system.out.println("*** " + frenchnumbertowords.convert(21)); system.out.println("*** " + frenchnumbertowords.convert(28)); system.out.println("*** " + frenchnumbertowords.convert(71)); system.out.println("*** " + frenchnumbertowords.convert(72)); system.out.println("*** " + frenchnumbertowords.convert(80)); system.out.println("*** " + frenchnumbertowords.convert(81)); system.out.println("*** " + frenchnumbertowords.convert(89)); system.out.println("*** " + frenchnumbertowords.convert(90)); system.out.println("*** " + frenchnumbertowords.convert(91)); system.out.println("*** " + frenchnumbertowords.convert(97)); system.out.println("*** " + frenchnumbertowords.convert(100)); system.out.println("*** " + frenchnumbertowords.convert(101)); system.out.println("*** " + frenchnumbertowords.convert(110)); system.out.println("*** " + frenchnumbertowords.convert(120)); system.out.println("*** " + frenchnumbertowords.convert(200)); system.out.println("*** " + frenchnumbertowords.convert(201)); system.out.println("*** " + frenchnumbertowords.convert(232)); system.out.println("*** " + frenchnumbertowords.convert(999)); system.out.println("*** " + frenchnumbertowords.convert(1000)); system.out.println("*** " + frenchnumbertowords.convert(1001)); system.out.println("*** " + frenchnumbertowords.convert(10000)); system.out.println("*** " + frenchnumbertowords.convert(10001)); system.out.println("*** " + frenchnumbertowords.convert(100000)); system.out.println("*** " + frenchnumbertowords.convert(2000000)); system.out.println("*** " + frenchnumbertowords.convert(3000000000l)); system.out.println("*** " + frenchnumbertowords.convert(2147483647)); /* *** output *** zéro *** neuf *** dix-neuf *** vingt et un *** vingt-huit *** soixante et onze *** soixante-douze *** quatre-vingt *** quatre-vingt-un *** quatre-vingt-neuf *** quatre-vingt-dix *** quatre-vingt-onze *** quatre-vingt-dix-sept *** cent *** cent un *** cent dix *** cent vingt *** deux cents *** deux cent un *** deux cent trente-deux *** neuf cent quatre-vingt-dix-neuf *** mille *** mille un *** dix mille *** dix mille un *** cent mille *** deux millions *** trois milliards *** deux milliards cent quarante-sept millions ** quatre cent quatre-vingt-trois mille 6 cent quarante-sept */ } }
you can handle "dollar , cent" conversion calling "convert" method 2 times.
string phrase = "12345.67" ; float num = new float( phrase ) ; int dollars = (int)math.floor( num ) ; int cent = (int)math.floor( ( num - dollars ) * 100.0f ) ; string s = "$ " + englishnumbertowords.convert( dollars ) + " , " + englishnumbertowords.convert( cent ) + " cents" ;
another way use built-in function of dbms (if available). oracle
sql> select to_char(to_date(873,'j'), 'jsp') converted_form dual; converted_form --------------------------- 8 hundred seventy-three sql> 'jsp' means : j : julian format. sp : spells word number passed to_date
Comments
Post a Comment