numbers_af.js 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. import { NUMBERS as NUMB } from '../messages.js';
  2. function hundredsToWords_(num) {
  3. let n = num % 1000;
  4. let str = '';
  5. let ones = NUMBERS.ones[Math.floor(n / 100)];
  6. str += ones ? ones + NUMBERS.numSep + 'honderd' : '';
  7. n = n % 100;
  8. if (n) {
  9. str += str ? NUMBERS.numSep : '';
  10. ones = NUMBERS.ones[n];
  11. if (ones) {
  12. str += ones;
  13. }
  14. else {
  15. const tens = NUMBERS.tens[Math.floor(n / 10)];
  16. ones = NUMBERS.ones[n % 10];
  17. str += ones ? ones + '-en-' + tens : tens;
  18. }
  19. }
  20. return str;
  21. }
  22. function numberToWords(num) {
  23. if (num === 0) {
  24. return NUMBERS.zero;
  25. }
  26. if (num >= Math.pow(10, 36)) {
  27. return num.toString();
  28. }
  29. let pos = 0;
  30. let str = '';
  31. while (num > 0) {
  32. const hundreds = num % 1000;
  33. if (hundreds) {
  34. const hund = hundredsToWords_(num % 1000);
  35. if (pos) {
  36. const large = NUMBERS.large[pos];
  37. str = hund + NUMBERS.numSep + large + (str ? NUMBERS.numSep + str : '');
  38. }
  39. else {
  40. str = hund + (str ? NUMBERS.numSep + str : '');
  41. }
  42. }
  43. num = Math.floor(num / 1000);
  44. pos++;
  45. }
  46. return str;
  47. }
  48. function numberToOrdinal(num, plural) {
  49. if (num === 1) {
  50. return 'enkel';
  51. }
  52. if (num === 2) {
  53. return plural ? 'helftes' : 'helfte';
  54. }
  55. if (num === 4) {
  56. return plural ? 'kwarte' : 'kwart';
  57. }
  58. return wordOrdinal(num) + (plural ? 's' : '');
  59. }
  60. function wordOrdinal(num) {
  61. if (num === 1) {
  62. return 'eerste';
  63. }
  64. if (num === 3) {
  65. return 'derde';
  66. }
  67. if (num === 8) {
  68. return 'agste';
  69. }
  70. if (num === 9) {
  71. return 'negende';
  72. }
  73. const ordinal = numberToWords(num);
  74. return ordinal + (num < 19 ? 'de' : 'ste');
  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. });