numbers_en.js 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. import { NUMBERS as NUMB } from '../messages.js';
  2. function hundredsToWords_(num) {
  3. let n = num % 1000;
  4. let str = '';
  5. str += NUMBERS.ones[Math.floor(n / 100)]
  6. ? NUMBERS.ones[Math.floor(n / 100)] + NUMBERS.numSep + 'hundred'
  7. : '';
  8. n = n % 100;
  9. if (n) {
  10. str += str ? NUMBERS.numSep : '';
  11. str +=
  12. NUMBERS.ones[n] ||
  13. NUMBERS.tens[Math.floor(n / 10)] +
  14. (n % 10 ? NUMBERS.numSep + NUMBERS.ones[n % 10] : '');
  15. }
  16. return str;
  17. }
  18. function numberToWords(num) {
  19. if (num === 0) {
  20. return NUMBERS.zero;
  21. }
  22. if (num >= Math.pow(10, 36)) {
  23. return num.toString();
  24. }
  25. let pos = 0;
  26. let str = '';
  27. while (num > 0) {
  28. const hundreds = num % 1000;
  29. if (hundreds) {
  30. str =
  31. hundredsToWords_(num % 1000) +
  32. (pos ? '-' + NUMBERS.large[pos] + '-' : '') +
  33. str;
  34. }
  35. num = Math.floor(num / 1000);
  36. pos++;
  37. }
  38. return str.replace(/-$/, '');
  39. }
  40. function numberToOrdinal(num, plural) {
  41. if (num === 1) {
  42. return plural ? 'oneths' : 'oneth';
  43. }
  44. if (num === 2) {
  45. return plural ? 'halves' : 'half';
  46. }
  47. const ordinal = wordOrdinal(num);
  48. return plural ? ordinal + 's' : ordinal;
  49. }
  50. function wordOrdinal(num) {
  51. let ordinal = numberToWords(num);
  52. if (ordinal.match(/one$/)) {
  53. ordinal = ordinal.slice(0, -3) + 'first';
  54. }
  55. else if (ordinal.match(/two$/)) {
  56. ordinal = ordinal.slice(0, -3) + 'second';
  57. }
  58. else if (ordinal.match(/three$/)) {
  59. ordinal = ordinal.slice(0, -5) + 'third';
  60. }
  61. else if (ordinal.match(/five$/)) {
  62. ordinal = ordinal.slice(0, -4) + 'fifth';
  63. }
  64. else if (ordinal.match(/eight$/)) {
  65. ordinal = ordinal.slice(0, -5) + 'eighth';
  66. }
  67. else if (ordinal.match(/nine$/)) {
  68. ordinal = ordinal.slice(0, -4) + 'ninth';
  69. }
  70. else if (ordinal.match(/twelve$/)) {
  71. ordinal = ordinal.slice(0, -6) + 'twelfth';
  72. }
  73. else if (ordinal.match(/ty$/)) {
  74. ordinal = ordinal.slice(0, -2) + 'tieth';
  75. }
  76. else {
  77. ordinal = ordinal + 'th';
  78. }
  79. return ordinal;
  80. }
  81. function numericOrdinal(num) {
  82. const tens = num % 100;
  83. const numStr = num.toString();
  84. if (tens > 10 && tens < 20) {
  85. return numStr + 'th';
  86. }
  87. switch (num % 10) {
  88. case 1:
  89. return numStr + 'st';
  90. case 2:
  91. return numStr + 'nd';
  92. case 3:
  93. return numStr + 'rd';
  94. default:
  95. return numStr + 'th';
  96. }
  97. }
  98. export const NUMBERS = NUMB({
  99. wordOrdinal: wordOrdinal,
  100. numericOrdinal: numericOrdinal,
  101. numberToWords: numberToWords,
  102. numberToOrdinal: numberToOrdinal
  103. });