minafaw3

FrenchNumberToWords

Jan 16th, 2016
189
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.39 KB | None | 0 0
  1. package com.rgagnon.howto;
  2.  
  3. import java.text.*;
  4.  
  5. class FrenchNumberToWords {
  6. private static final String[] dizaineNames = {
  7. "",
  8. "",
  9. "vingt",
  10. "trente",
  11. "quarante",
  12. "cinquante",
  13. "soixante",
  14. "soixante",
  15. "quatre-vingt",
  16. "quatre-vingt"
  17. };
  18.  
  19. private static final String[] uniteNames1 = {
  20. "",
  21. "un",
  22. "deux",
  23. "trois",
  24. "quatre",
  25. "cinq",
  26. "six",
  27. "sept",
  28. "huit",
  29. "neuf",
  30. "dix",
  31. "onze",
  32. "douze",
  33. "treize",
  34. "quatorze",
  35. "quinze",
  36. "seize",
  37. "dix-sept",
  38. "dix-huit",
  39. "dix-neuf"
  40. };
  41.  
  42. private static final String[] uniteNames2 = {
  43. "",
  44. "",
  45. "deux",
  46. "trois",
  47. "quatre",
  48. "cinq",
  49. "six",
  50. "sept",
  51. "huit",
  52. "neuf",
  53. "dix"
  54. };
  55.  
  56. private FrenchNumberToWords() {}
  57.  
  58. private static String convertZeroToHundred(int number) {
  59.  
  60. int laDizaine = number / 10;
  61. int lUnite = number % 10;
  62. String resultat = "";
  63.  
  64. switch (laDizaine) {
  65. case 1 :
  66. case 7 :
  67. case 9 :
  68. lUnite = lUnite + 10;
  69. break;
  70. default:
  71. }
  72.  
  73. // s�parateur "-" "et" ""
  74. String laLiaison = "";
  75. if (laDizaine > 1) {
  76. laLiaison = "-";
  77. }
  78. // cas particuliers
  79. switch (lUnite) {
  80. case 0:
  81. laLiaison = "";
  82. break;
  83. case 1 :
  84. if (laDizaine == 8) {
  85. laLiaison = "-";
  86. }
  87. else {
  88. laLiaison = " et ";
  89. }
  90. break;
  91. case 11 :
  92. if (laDizaine==7) {
  93. laLiaison = " et ";
  94. }
  95. break;
  96. default:
  97. }
  98.  
  99. // dizaines en lettres
  100. switch (laDizaine) {
  101. case 0:
  102. resultat = uniteNames1[lUnite];
  103. break;
  104. case 8 :
  105. if (lUnite == 0) {
  106. resultat = dizaineNames[laDizaine];
  107. }
  108. else {
  109. resultat = dizaineNames[laDizaine]
  110. + laLiaison + uniteNames1[lUnite];
  111. }
  112. break;
  113. default :
  114. resultat = dizaineNames[laDizaine]
  115. + laLiaison + uniteNames1[lUnite];
  116. }
  117. return resultat;
  118. }
  119.  
  120. private static String convertLessThanOneThousand(int number) {
  121.  
  122. int lesCentaines = number / 100;
  123. int leReste = number % 100;
  124. String sReste = convertZeroToHundred(leReste);
  125.  
  126. String resultat;
  127. switch (lesCentaines) {
  128. case 0:
  129. resultat = sReste;
  130. break;
  131. case 1 :
  132. if (leReste > 0) {
  133. resultat = "cent " + sReste;
  134. }
  135. else {
  136. resultat = "cent";
  137. }
  138. break;
  139. default :
  140. if (leReste > 0) {
  141. resultat = uniteNames2[lesCentaines] + " cent " + sReste;
  142. }
  143. else {
  144. resultat = uniteNames2[lesCentaines] + " cents";
  145. }
  146. }
  147. return resultat;
  148. }
  149.  
  150. public static String convert(long number) {
  151. // 0 � 999 999 999 999
  152. if (number == 0) { return "zéro"; }
  153.  
  154. String snumber = Long.toString(number);
  155.  
  156. // pad des "0"
  157. String mask = "000000000000";
  158. DecimalFormat df = new DecimalFormat(mask);
  159. snumber = df.format(number);
  160.  
  161. // XXXnnnnnnnnn
  162. int lesMilliards = Integer.parseInt(snumber.substring(0,3));
  163. // nnnXXXnnnnnn
  164. int lesMillions = Integer.parseInt(snumber.substring(3,6));
  165. // nnnnnnXXXnnn
  166. int lesCentMille = Integer.parseInt(snumber.substring(6,9));
  167. // nnnnnnnnnXXX
  168. int lesMille = Integer.parseInt(snumber.substring(9,12));
  169.  
  170. String tradMilliards;
  171. switch (lesMilliards) {
  172. case 0:
  173. tradMilliards = "";
  174. break;
  175. case 1 :
  176. tradMilliards = convertLessThanOneThousand(lesMilliards)
  177. + " milliard ";
  178. break;
  179. default :
  180. tradMilliards = convertLessThanOneThousand(lesMilliards)
  181. + " milliards ";
  182. }
  183. String resultat = tradMilliards;
  184.  
  185. String tradMillions;
  186. switch (lesMillions) {
  187. case 0:
  188. tradMillions = "";
  189. break;
  190. case 1 :
  191. tradMillions = convertLessThanOneThousand(lesMillions)
  192. + " million ";
  193. break;
  194. default :
  195. tradMillions = convertLessThanOneThousand(lesMillions)
  196. + " millions ";
  197. }
  198. resultat = resultat + tradMillions;
  199.  
  200. String tradCentMille;
  201. switch (lesCentMille) {
  202. case 0:
  203. tradCentMille = "";
  204. break;
  205. case 1 :
  206. tradCentMille = "mille ";
  207. break;
  208. default :
  209. tradCentMille = convertLessThanOneThousand(lesCentMille)
  210. + " mille ";
  211. }
  212. resultat = resultat + tradCentMille;
  213.  
  214. String tradMille;
  215. tradMille = convertLessThanOneThousand(lesMille);
  216. resultat = resultat + tradMille;
  217.  
  218. return resultat;
  219. }
  220.  
  221. public static void main(String[] args) {
  222. System.out.println("*** " + FrenchNumberToWords.convert(0));
  223. System.out.println("*** " + FrenchNumberToWords.convert(9));
  224. System.out.println("*** " + FrenchNumberToWords.convert(19));
  225. System.out.println("*** " + FrenchNumberToWords.convert(21));
  226. System.out.println("*** " + FrenchNumberToWords.convert(28));
  227. System.out.println("*** " + FrenchNumberToWords.convert(71));
  228. System.out.println("*** " + FrenchNumberToWords.convert(72));
  229. System.out.println("*** " + FrenchNumberToWords.convert(80));
  230. System.out.println("*** " + FrenchNumberToWords.convert(81));
  231. System.out.println("*** " + FrenchNumberToWords.convert(89));
  232. System.out.println("*** " + FrenchNumberToWords.convert(90));
  233. System.out.println("*** " + FrenchNumberToWords.convert(91));
  234. System.out.println("*** " + FrenchNumberToWords.convert(97));
  235. System.out.println("*** " + FrenchNumberToWords.convert(100));
  236. System.out.println("*** " + FrenchNumberToWords.convert(101));
  237. System.out.println("*** " + FrenchNumberToWords.convert(110));
  238. System.out.println("*** " + FrenchNumberToWords.convert(120));
  239. System.out.println("*** " + FrenchNumberToWords.convert(200));
  240. System.out.println("*** " + FrenchNumberToWords.convert(201));
  241. System.out.println("*** " + FrenchNumberToWords.convert(232));
  242. System.out.println("*** " + FrenchNumberToWords.convert(999));
  243. System.out.println("*** " + FrenchNumberToWords.convert(1000));
  244. System.out.println("*** " + FrenchNumberToWords.convert(1001));
  245. System.out.println("*** " + FrenchNumberToWords.convert(10000));
  246. System.out.println("*** " + FrenchNumberToWords.convert(10001));
  247. System.out.println("*** " + FrenchNumberToWords.convert(100000));
  248. System.out.println("*** " + FrenchNumberToWords.convert(2000000));
  249. System.out.println("*** " + FrenchNumberToWords.convert(3000000000L));
  250. System.out.println("*** " + FrenchNumberToWords.convert(2147483647));
  251. /*
  252. *** OUTPUT
  253. *** zéro
  254. *** neuf
  255. *** dix-neuf
  256. *** vingt et un
  257. *** vingt-huit
  258. *** soixante et onze
  259. *** soixante-douze
  260. *** quatre-vingt
  261. *** quatre-vingt-un
  262. *** quatre-vingt-neuf
  263. *** quatre-vingt-dix
  264. *** quatre-vingt-onze
  265. *** quatre-vingt-dix-sept
  266. *** cent
  267. *** cent un
  268. *** cent dix
  269. *** cent vingt
  270. *** deux cents
  271. *** deux cent un
  272. *** deux cent trente-deux
  273. *** neuf cent quatre-vingt-dix-neuf
  274. *** mille
  275. *** mille un
  276. *** dix mille
  277. *** dix mille un
  278. *** cent mille
  279. *** deux millions
  280. *** trois milliards
  281. *** deux milliards cent quarante-sept millions
  282. ** quatre cent quatre-vingt-trois mille six cent quarante-sept
  283. */
  284. }
  285. }
Add Comment
Please, Sign In to add comment