JavaScript numbers to Words

前端 未结 24 1233
死守一世寂寞
死守一世寂寞 2020-11-22 14:39

I\'m trying to convert numbers into english words, for example 1234 would become: \"one thousand two hundred thirty four\".

My Tact

24条回答
  •  栀梦
    栀梦 (楼主)
    2020-11-22 14:58

    Here is another version from me with some unit tests.

    Don't use it with numbers larger than Number.MAX_SAFE_INTEGER.

    describe("English Numerals Converter", function () {
      assertNumeral(0, "zero");
      assertNumeral(1, "one");
      assertNumeral(2, "two");
      assertNumeral(3, "three");
      assertNumeral(4, "four");
      assertNumeral(5, "five");
      assertNumeral(6, "six");
      assertNumeral(7, "seven");
      assertNumeral(8, "eight");
      assertNumeral(9, "nine");
      assertNumeral(10, "ten");
      assertNumeral(11, "eleven");
      assertNumeral(12, "twelve");
      assertNumeral(13, "thirteen");
      assertNumeral(14, "fourteen");
      assertNumeral(15, "fifteen");
      assertNumeral(16, "sixteen");
      assertNumeral(17, "seventeen");
      assertNumeral(18, "eighteen");
      assertNumeral(19, "nineteen");
      assertNumeral(20, "twenty");
      assertNumeral(21, "twenty-one");
      assertNumeral(22, "twenty-two");
      assertNumeral(23, "twenty-three");
      assertNumeral(30, "thirty");
      assertNumeral(37, "thirty-seven");
      assertNumeral(40, "forty");
      assertNumeral(50, "fifty");
      assertNumeral(60, "sixty");
      assertNumeral(70, "seventy");
      assertNumeral(80, "eighty");
      assertNumeral(90, "ninety");
      assertNumeral(99, "ninety-nine");
      assertNumeral(100, "one hundred");
      assertNumeral(101, "one hundred and one");
      assertNumeral(102, "one hundred and two");
      assertNumeral(110, "one hundred and ten");
      assertNumeral(120, "one hundred and twenty");
      assertNumeral(121, "one hundred and twenty-one");
      assertNumeral(199, "one hundred and ninety-nine");
      assertNumeral(200, "two hundred");
      assertNumeral(999, "nine hundred and ninety-nine");
      assertNumeral(1000, "one thousand");
      assertNumeral(1001, "one thousand and one");
      assertNumeral(1011, "one thousand and eleven");
      assertNumeral(1111, "one thousand and one hundred and eleven");
      assertNumeral(9999, "nine thousand and nine hundred and ninety-nine");
      assertNumeral(10000, "ten thousand");
      assertNumeral(20000, "twenty thousand");
      assertNumeral(21000, "twenty-one thousand");
      assertNumeral(90000, "ninety thousand");
      assertNumeral(90001, "ninety thousand and one");
      assertNumeral(90100, "ninety thousand and one hundred");
      assertNumeral(90901, "ninety thousand and nine hundred and one");
      assertNumeral(90991, "ninety thousand and nine hundred and ninety-one");
      assertNumeral(90999, "ninety thousand and nine hundred and ninety-nine");
      assertNumeral(91000, "ninety-one thousand");
      assertNumeral(99999, "ninety-nine thousand and nine hundred and ninety-nine");
      assertNumeral(100000, "one hundred thousand");
      assertNumeral(999000, "nine hundred and ninety-nine thousand");
      assertNumeral(1000000, "one million");
      assertNumeral(10000000, "ten million");
      assertNumeral(100000000, "one hundred million");
      assertNumeral(1000000000, "one billion");
      assertNumeral(1000000000000, "one trillion");
      assertNumeral(1000000000000000, "one quadrillion");
      assertNumeral(1000000000000000000, "one quintillion");
      assertNumeral(1000000000000000000000, "one sextillion");
    
      assertNumeral(-1, "minus one");
      assertNumeral(-999, "minus nine hundred and ninety-nine");
    
      function assertNumeral(number, numeral) {
        it(number + " is " + numeral, function () {
          expect(convert(number)).toBe(numeral);
        });
      }
    });
    
    
    function convert(n) {
      let NUMERALS = [
        {value: 1000000000000000000000, str: "sextillion"},
        {value: 1000000000000000000, str: "quintillion"},
        {value: 1000000000000000, str: "quadrillion"},
        {value: 1000000000000, str: "trillion"},
        {value: 1000000000, str: "billion"},
        {value: 1000000, str: "million"},
        {value: 1000, str: "thousand"},
        {value: 100, str: "hundred"},
        {value: 90, str: "ninety"},
        {value: 80, str: "eighty"},
        {value: 70, str: "seventy"},
        {value: 60, str: "sixty"},
        {value: 50, str: "fifty"},
        {value: 40, str: "forty"},
        {value: 30, str: "thirty"},
        {value: 20, str: "twenty"},
        {value: 19, str: "nineteen"},
        {value: 18, str: "eighteen"},
        {value: 17, str: "seventeen"},
        {value: 16, str: "sixteen"},
        {value: 15, str: "fifteen"},
        {value: 14, str: "fourteen"},
        {value: 13, str: "thirteen"},
        {value: 12, str: "twelve"},
        {value: 11, str: "eleven"},
        {value: 10, str: "ten"},
        {value: 9, str: "nine"},
        {value: 8, str: "eight"},
        {value: 7, str: "seven"},
        {value: 6, str: "six"},
        {value: 5, str: "five"},
        {value: 4, str: "four"},
        {value: 3, str: "three"},
        {value: 2, str: "two"},
        {value: 1, str: "one"}
      ];
    
      if (n < 0) {
        return "minus " + convert(-n);
      } else if (n === 0) {
        return "zero";
      } else {
        let result = "";
        for (let numeral of NUMERALS) {
          if (n >= numeral.value) {
            if (n < 100) {
              result += numeral.str;
              n -= numeral.value;
              if (n > 0) result += "-";
            } else {
              let times = Math.floor(n / numeral.value);
              result += convert(times) + " " + numeral.str;
              n -= numeral.value * times;
              if (n > 0) result += " and ";
            }
          }
        }
        return result;
      }
    }
    

提交回复
热议问题