代码之家  ›  专栏  ›  技术社区  ›  REDDY PRASAD Mortana

在javascript中,将数字从10.0改为0.01

  •  0
  • REDDY PRASAD Mortana  · 技术社区  · 6 年前

    我正在尝试实现一个函数,它在javascript中反转给定的数字(int/float)。功能实现是

    function reverseNumber(n) {
        let reversed = 0;
        const sign = Math.sign(n)
        n *= sign;
    
        const exponent = n.toString().indexOf('.');
        if (exponent !== -1) n *= Math.pow(10, n.toString().length - 1 - exponent);
        while (n !== 0) {
            reversed *= 10;
            reversed += n % 10;
            n = Math.floor(n / 10);
        }
        if (exponent !== -1) reversed /= Math.pow(10, exponent);
        return reversed * sign;
    }
    

    此函数通过以下测试用例

      expect(reverseNumber(0)).toEqual(0);
      expect(reverseNumber(15)).toEqual(51);
      expect(reverseNumber(90)).toEqual(9);
      expect(reverseNumber(-5)).toEqual(-5);
      expect(reverseNumber(-90)).toEqual(-9);
      expect(reverseNumber(-2359)).toEqual(-9532);
      expect(reverseNumber(12.403)).toEqual(304.21);
      expect(reverseNumber(-12.4)).toEqual(-4.21);
    

    唯一失败的情况是当输入 10.0 . 测试用例是

    expect(reverseNumber(10.0)).toEqual(0.01);
    

    我找不到任何方法来逆转 = & gt; 0.01 . 你们能帮忙吗? 谢谢

    2 回复  |  直到 6 年前
        1
  •  6
  •   T.J. Crowder    6 年前

    function reverseNumber(num) {
        // Remember if it's negative
        var isNegative = num < 0;
        // Get the digits and decimal just for the absolute part
        var chars = (isNegative ? -num : num).toString().split("");
        // Reverse them
        chars.reverse();
        // Put it back together and convert back to a number
        var num = Number(chars.join(""));
        // Return the result, converting back to negative if appropriate
        return isNegative ? -num : num;
    }
    

    function reverseNumber(num) {
        // Remember if it's negative
        var isNegative = num < 0;
        // Get the digits and decimal just for the absolute part
        var chars = (isNegative ? -num : num).toString().split("");
        // Reverse them
        chars.reverse();
        // Put it back together and convert back to a number
        var num = Number(chars.join(""));
        // Return the result, converting back to negative if appropriate
        return isNegative ? -num : num;
    }
    
    function test(num, expect) {
        var result = reverseNumber(num);
        console.log(num, " => ", result, result == expect ? "OK" : "ERROR");
    }
    
    test(0, 0);
    test(15, 51);
    test(90, 9);
    test(-5, -5);
    test(-90, -9);
    test(-2359, -9532);
    test(12.403, 304.21);
    test(-12.4, -4.21);
    .as-console-wrapper {
      max-height: 100% !important;
    }

    expect(reverseNumber(10.0)).toEqual(0.01);
    

    10 10.0 0.01 15 15.0 0.51 51

    function reverseNumber(numStr) {
        // Remember if it's negative
        var isNegative = numStr[0] === "-";
        // Get the digits and decimal just for the absolute part
        var chars = (isNegative ? numStr.substring(1) : numStr).split("");
        // Reverse them
        chars.reverse();
        // Put it back together and convert back to a number
        var num = Number(chars.join(""));
        // Return the result, converting back to negative if appropriate
        return String(isNegative ? -num : num);
    }
    
    function test(num, expect) {
        var result = reverseNumber(num);
        console.log(num, " => ", result, result == expect ? "OK" : "ERROR");
    }
    
    test("0", "0");
    test("15", "51");
    test("90", "9");
    test("-5", "-5");
    test("-90", "-9");
    test("-2359", "-9532");
    test("12.403", "304.21");
    test("-12.4", "-4.21");
    test("10.0", "0.01");
        2
  •  0
  •   Get Off My Lawn    6 年前

    10.0 10 == 10.0 = true

    function reverseIt(num) {
      let neg = num < 0
      let result = num.toString().replace(/^-/, '').split('').reverse().join('')
      return parseFloat(neg ? '-' + result : result)
    }
    
    console.log(reverseIt(123))
    console.log(reverseIt(10))
    console.log(reverseIt(1235))
    console.log(reverseIt(-5310))
    console.log(reverseIt(10.0.toFixed(1)))