numbers_ko.js 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. import { NUMBERS as NUMB } from '../messages.js';
  2. function thousandsToWords_(num) {
  3. let n = num % 10000;
  4. let str = '';
  5. str += NUMBERS.ones[Math.floor(n / 1000)]
  6. ? Math.floor(n / 1000) === 1
  7. ? '천'
  8. : NUMBERS.ones[Math.floor(n / 1000)] + '천'
  9. : '';
  10. n = n % 1000;
  11. if (n) {
  12. str += NUMBERS.ones[Math.floor(n / 100)]
  13. ? Math.floor(n / 100) === 1
  14. ? '백'
  15. : NUMBERS.ones[Math.floor(n / 100)] + '백'
  16. : '';
  17. n = n % 100;
  18. str +=
  19. NUMBERS.tens[Math.floor(n / 10)] + (n % 10 ? NUMBERS.ones[n % 10] : '');
  20. }
  21. return str;
  22. }
  23. function numberToWords(num) {
  24. if (num === 0)
  25. return NUMBERS.zero;
  26. if (num >= Math.pow(10, 36))
  27. return num.toString();
  28. let pos = 0;
  29. let str = '';
  30. while (num > 0) {
  31. const thousands = num % 10000;
  32. if (thousands) {
  33. str =
  34. thousandsToWords_(num % 10000) +
  35. (pos ? NUMBERS.large[pos] + NUMBERS.numSep : '') +
  36. str;
  37. }
  38. num = Math.floor(num / 10000);
  39. pos++;
  40. }
  41. return str.replace(/ $/, '');
  42. }
  43. function numberToOrdinal(num, _plural) {
  44. if (num === 1)
  45. return '첫번째';
  46. return wordOrdinal(num) + '번째';
  47. }
  48. function wordOrdinal(num) {
  49. const ordinal = numberToWords(num);
  50. num %= 100;
  51. const label = numberToWords(num);
  52. if (!label || !num)
  53. return ordinal;
  54. const tens = num === 20 ? '스무' : NUMBERS.tens[10 + Math.floor(num / 10)];
  55. const ones = NUMBERS.ones[10 + Math.floor(num % 10)];
  56. return ordinal.slice(0, -label.length) + tens + ones;
  57. }
  58. function numericOrdinal(num) {
  59. return numberToOrdinal(num, false);
  60. }
  61. export const NUMBERS = NUMB({
  62. wordOrdinal: wordOrdinal,
  63. numericOrdinal: numericOrdinal,
  64. numberToWords: numberToWords,
  65. numberToOrdinal: numberToOrdinal
  66. });