Reverse decimal digits in javascript

后端 未结 14 919
灰色年华
灰色年华 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.

提交回复
热议问题