Reverse decimal digits in javascript

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

提交回复
热议问题