Get the name of a number, even if it's stupidly big.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
This repo is archived. You can view files and clone it, but cannot push or open issues/pull-requests.

67 rivejä
1.8 KiB

  1. import NAMES from '../../names.json'
  2. import ones from './ones'
  3. import tens from './tens'
  4. import hundreds from './hundreds'
  5. interface CombiningPrefix {
  6. (kiloHundreds: number, kiloTens: number, kiloOnes: number, kiloPower?: number): string
  7. }
  8. const combiningPrefix: CombiningPrefix = (kiloHundredsRaw, kiloTens, kiloOnes, kiloPower = 0) => {
  9. let kiloHundreds = kiloHundredsRaw
  10. let prefix = ''
  11. const isMillia = kiloHundredsRaw >= 10
  12. if (isMillia) {
  13. prefix += combiningPrefix(
  14. Math.floor(kiloHundredsRaw / 1000),
  15. Math.floor((kiloHundredsRaw / 100) % 10),
  16. Math.floor((kiloHundredsRaw / 10) % 10),
  17. kiloPower + 1,
  18. )
  19. kiloHundreds = kiloHundredsRaw % 10
  20. }
  21. const hasHundreds = kiloHundreds > 0
  22. if (hasHundreds) {
  23. prefix += hundreds(kiloHundreds)
  24. }
  25. if (
  26. kiloPower > 0 &&
  27. ((kiloHundreds === 0 && kiloTens === 0 && kiloOnes > 1) ||
  28. ((kiloHundreds > 0 || kiloTens > 0) && kiloOnes > 0) ||
  29. (kiloHundreds === 0 && kiloTens === 0 && kiloOnes === 1 && prefix.endsWith(NAMES.kiloThousand)))
  30. ) {
  31. // http://www.isthe.com/chongo/tech/math/number/howhigh.html
  32. // Section: Titanic size numbers < 10^3000003
  33. prefix += ones(kiloOnes, false)
  34. }
  35. if (kiloPower === 0 && kiloOnes > 0) {
  36. const special = kiloHundreds === 0 && kiloTens === 0
  37. prefix += ones(kiloOnes, special)
  38. if (special && [5, 6].includes(kiloOnes)) {
  39. prefix += 't'
  40. }
  41. }
  42. if (kiloTens > 0) {
  43. prefix += tens(kiloTens)
  44. }
  45. if (kiloPower > 0) {
  46. if (!(kiloHundreds === 0 && kiloTens === 0 && kiloOnes === 0)) {
  47. for (let p = 0; p < kiloPower; p += 1) {
  48. prefix += NAMES.kiloThousand
  49. }
  50. }
  51. return prefix
  52. }
  53. if ((kiloHundreds === 0 && kiloTens > 1) || (kiloHundreds > 0 && kiloTens !== 1) || kiloHundredsRaw >= 10) {
  54. prefix += 't'
  55. }
  56. return prefix + 'i'
  57. }
  58. export default combiningPrefix