numbers_de.js 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. import { NUMBERS as NUMB } from '../messages.js';
  2. function onePrefix_(num, mill = false) {
  3. return num === NUMBERS.ones[1] ? (mill ? 'eine' : 'ein') : num;
  4. }
  5. function hundredsToWords_(num) {
  6. let n = num % 1000;
  7. let str = '';
  8. let ones = NUMBERS.ones[Math.floor(n / 100)];
  9. str += ones ? onePrefix_(ones) + 'hundert' : '';
  10. n = n % 100;
  11. if (n) {
  12. str += str ? NUMBERS.numSep : '';
  13. ones = NUMBERS.ones[n];
  14. if (ones) {
  15. str += ones;
  16. }
  17. else {
  18. const tens = NUMBERS.tens[Math.floor(n / 10)];
  19. ones = NUMBERS.ones[n % 10];
  20. str += ones ? onePrefix_(ones) + 'und' + tens : tens;
  21. }
  22. }
  23. return str;
  24. }
  25. function numberToWords(num) {
  26. if (num === 0) {
  27. return NUMBERS.zero;
  28. }
  29. if (num >= Math.pow(10, 36)) {
  30. return num.toString();
  31. }
  32. let pos = 0;
  33. let str = '';
  34. while (num > 0) {
  35. const hundreds = num % 1000;
  36. if (hundreds) {
  37. const hund = hundredsToWords_(num % 1000);
  38. if (pos) {
  39. const large = NUMBERS.large[pos];
  40. const plural = pos > 1 && hundreds > 1 ? (large.match(/e$/) ? 'n' : 'en') : '';
  41. str = onePrefix_(hund, pos > 1) + large + plural + str;
  42. }
  43. else {
  44. str = onePrefix_(hund, pos > 1) + str;
  45. }
  46. }
  47. num = Math.floor(num / 1000);
  48. pos++;
  49. }
  50. return str.replace(/ein$/, 'eins');
  51. }
  52. function numberToOrdinal(num, plural) {
  53. if (num === 1) {
  54. return 'eintel';
  55. }
  56. if (num === 2) {
  57. return plural ? 'halbe' : 'halb';
  58. }
  59. return wordOrdinal(num) + 'l';
  60. }
  61. function wordOrdinal(num) {
  62. if (num === 1) {
  63. return 'erste';
  64. }
  65. if (num === 3) {
  66. return 'dritte';
  67. }
  68. if (num === 7) {
  69. return 'siebte';
  70. }
  71. if (num === 8) {
  72. return 'achte';
  73. }
  74. const ordinal = numberToWords(num);
  75. return ordinal + (num < 19 ? 'te' : 'ste');
  76. }
  77. function numericOrdinal(num) {
  78. return num.toString() + '.';
  79. }
  80. export const NUMBERS = NUMB({
  81. wordOrdinal: wordOrdinal,
  82. numericOrdinal: numericOrdinal,
  83. numberToWords: numberToWords,
  84. numberToOrdinal: numberToOrdinal
  85. });