numbers_da.js 2.4 KB

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