numbers_hi.js 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. import { Grammar } from '../../rule_engine/grammar.js';
  2. import { NUMBERS as NUMB } from '../messages.js';
  3. function hundredsToWords_(num) {
  4. let n = num % 1000;
  5. let str = '';
  6. str += NUMBERS.ones[Math.floor(n / 100)]
  7. ? NUMBERS.ones[Math.floor(n / 100)] +
  8. NUMBERS.numSep +
  9. NUMBERS.special.hundred
  10. : '';
  11. n = n % 100;
  12. if (n) {
  13. str += str ? NUMBERS.numSep : '';
  14. str += NUMBERS.ones[n];
  15. }
  16. return str;
  17. }
  18. function numberToWords(num) {
  19. if (num === 0) {
  20. return NUMBERS.zero;
  21. }
  22. if (num >= Math.pow(10, 32)) {
  23. return num.toString();
  24. }
  25. let pos = 0;
  26. let str = '';
  27. const hundreds = num % 1000;
  28. const hundredsWords = hundredsToWords_(hundreds);
  29. num = Math.floor(num / 1000);
  30. if (!num) {
  31. return hundredsWords;
  32. }
  33. while (num > 0) {
  34. const thousands = num % 100;
  35. if (thousands) {
  36. str =
  37. NUMBERS.ones[thousands] +
  38. NUMBERS.numSep +
  39. NUMBERS.large[pos] +
  40. (str ? NUMBERS.numSep + str : '');
  41. }
  42. num = Math.floor(num / 100);
  43. pos++;
  44. }
  45. return hundredsWords ? str + NUMBERS.numSep + hundredsWords : str;
  46. }
  47. function numberToOrdinal(num, _plural) {
  48. if (num <= 10) {
  49. return NUMBERS.special.smallDenominators[num];
  50. }
  51. return wordOrdinal(num) + ' अंश';
  52. }
  53. function wordOrdinal(num) {
  54. const gender = Grammar.getInstance().getParameter('gender');
  55. if (num <= 0) {
  56. return num.toString();
  57. }
  58. if (num < 10) {
  59. return gender === 'f'
  60. ? NUMBERS.special.ordinalsFeminine[num]
  61. : NUMBERS.special.ordinalsMasculine[num];
  62. }
  63. const ordinal = numberToWords(num);
  64. return ordinal + (gender === 'f' ? 'वीं' : 'वाँ');
  65. }
  66. function numericOrdinal(num) {
  67. const gender = Grammar.getInstance().getParameter('gender');
  68. if (num > 0 && num < 10) {
  69. return gender === 'f'
  70. ? NUMBERS.special.simpleSmallOrdinalsFeminine[num]
  71. : NUMBERS.special.simpleSmallOrdinalsMasculine[num];
  72. }
  73. const ordinal = num
  74. .toString()
  75. .split('')
  76. .map(function (x) {
  77. const num = parseInt(x, 10);
  78. return isNaN(num) ? '' : NUMBERS.special.simpleNumbers[num];
  79. })
  80. .join('');
  81. return ordinal + (gender === 'f' ? 'वीं' : 'वाँ');
  82. }
  83. export const NUMBERS = NUMB({
  84. wordOrdinal: wordOrdinal,
  85. numericOrdinal: numericOrdinal,
  86. numberToWords: numberToWords,
  87. numberToOrdinal: numberToOrdinal
  88. });