Reverse decimal digits in javascript

后端 未结 14 934
灰色年华
灰色年华 2021-02-05 16:24

How do I reverse the digits of a number using bitwise?

input:

x = 123; 

output:

x = 321; 
相关标签:
14条回答
  • 2021-02-05 16:53

    Here is another way...

    var reversed = num.toString().split('').reverse().join('');
    

    jsFiddle.

    If you wanted it again as a Number, use parseInt(reversed, 10). Keep in mind though, leading 0s are not significant in a decimal number, and you will lose them if you convert to Number.

    0 讨论(0)
  • 2021-02-05 16:55

    OK, how about using and chaining these popular tricks in JavaScript in one-line function as below...

    const reverseNum = num => +("" + ~~num.split("").reverse().join(""));
    

    And call it like these:

    reverseNum(123); //321
    reverseNum(423.09); //324
    reverseNum(23305.1); //50332
    reverseNum(89112); //21198
    reverseNum(568434.2389); //434865
    
    0 讨论(0)
  • 2021-02-05 16:58

    Here are reversible array functions in JavaScript that handle integers or strings:

    function reverse(array)
    {
        var left = null;
        var right = null;
        var length = array.length;
        for (left = 0, right = length - 1; left < right; left += 1, right -= 1)
        {
            var temporary = array[left];
            array[left] = array[right];
            array[right] = temporary;
        }
        return array;
    }
    
    function toDigitsArrayFromInteger(integer, isReverse)
    {
        var digits = [];
    
        if (integer > 0)
        {
            var floor = window.Math.floor;
            while (integer > 0)
            {
                digits.push(floor(integer % 10));
                integer = floor(integer / 10);
            }
    
            // Array is populated in reverse order. Un-reverse it to make it normal.
            if (!isReverse)
            {
                digits = reverse(digits);
            }
        }
        else if (integer < 0)
        {
            digits = toDigitsArrayFromInteger(-integer, isReverse);
        }
        else if (integer === 0)
        {
            digits.push(0);
        }
    
        return digits;
    }
    
    function toDigitsArrayFromString(string, isReverse)
    {
        var digits = [];
    
        string += ""; // Coerce to string.
    
        var i = null;
        var length = string.length;
        for (i = 0; i < length; i += 1)
        {
            var integer = parseInt(string.charAt(i), 10);
            if (isFinite(integer))
            {
                digits.push(integer);
            }
        }
    
        if (isReverse)
        {
            digits = reverse(digits);
        }
    
        return digits;
    }
    

    Once you have the digits as an array, you can reverse the array easily to get the digits starting from the left or from the right.

    The string function is more versatile because it can find any digit in a string, whereas the integer function is limited to integers.

    Benchmarks: http://jsperf.com/todigitsarray

    The benchmarks between the two functions show that in Firefox 10 and Chrome 12, the string function is 30% to 60% faster than the integer function. In Opera 12, the integer function is slightly faster by about 10%.

    0 讨论(0)
  • 2021-02-05 16:58

    This takes Number x as a parameter and returns the reversed number.

    const reverse = (x) => Number(x.toString().split("").reverse().join(""));

    0 讨论(0)
  • 2021-02-05 17:04

    try this

    var n = 352;
    function loop(n, r){
        if(!n) return r;
        r = (r ? r * 10 : 0) + n % 10;
        return loop(Math.floor( n / 10), r);
    }
    console.log(loop(n));
    
    0 讨论(0)
  • 2021-02-05 17:05

    Simple and quick solution: Let's assume that you want to reverse a number 4546. You will take the reminder from each division by 10 and append it to the result until the number is > 0. And simultaneously updating the num variable by dividing it by 10.

    var x = '';
    var num = 4546;
    while(num>0){
     x = x + (num%10);
     num = parseInt(num/10);
    }
    console.log(x);
    
    0 讨论(0)
提交回复
热议问题