Javascript 在javascript中反转十进制数字
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6351825/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Reverse decimal digits in javascript
提问by The Mask
How do I reverse the digits of a number using bitwise?
如何使用按位反转数字的数字?
input:
输入:
x = 123;
output:
输出:
x = 321;
How Do this?
这怎么办?
回答by Ry-
That's not inverting bits; that's reversing the order of decimal digits, which is completely different. Here's one way:
这不是反转位;这是颠倒十进制数字的顺序,这是完全不同的。这是一种方法:
var x = 123;
var y = 0;
for(; x; x = Math.floor(x / 10)) {
y *= 10;
y += x % 10;
}
x = y;
If you actually want to invert bits, it's:
如果你真的想反转位,它是:
x = ~x;
As a function:
作为一个函数:
function reverse(n) {
for(var r = 0; n; n = Math.floor(n / 10)) {
r *= 10;
r += n % 10;
}
return r;
}
回答by Jason Gennaro
If you wanted to make a simple reversal:
如果您想进行简单的反转:
var x = 123;
var y = x.toString();
var z = y.split("").reverse().join("");
var aa = Number(z);
document.write(aa);
回答by alex
Here is another way...
这是另一种方式...
var reversed = num.toString().split('').reverse().join('');
If you wanted it again as a Number
, use parseInt(reversed, 10)
. Keep in mind though, leading 0
s are not significant in a decimal number, and you will lose them if you convert to Number
.
如果您再次想要它作为Number
,请使用parseInt(reversed, 10)
. 但请记住,前导0
s 在十进制数中并不重要,如果转换为Number
.
回答by Naresh Kumar
you also use this function
你也用这个功能
function myfunction(a){
var x=a.toString();
var y= x.split("");
var z=y.reverse();
var result=z.join("");
return result;
} myfunction(123);
我的功能(123);
回答by Nikhil.Hmath
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.
简单快速的解决方案:假设您想反转一个数字 4546。您将把每个除法的提醒除以 10 并将其附加到结果中,直到数字 > 0。同时通过将其除以 10 来更新 num 变量.
var x = '';
var num = 4546;
while(num>0){
x = x + (num%10);
num = parseInt(num/10);
}
console.log(x);
回答by bajran
Reversing The Positive/ Negative Integer Number
反转正/负整数
function reverseInt(n) {
return parseInt(n.toString().split('').reverse().join()) * Math.sign(n)
}
If n is -5, then Math.sign(n)==> will return -1
If n is 5, then Math.sign(n)==> will return 1
如果 n 是 -5,那么 Math.sign(n)==> 将返回 -1
如果 n 为 5,则 Math.sign(n)==> 将返回 1
回答by Kunle Babatunde
//reverse integer
const revInt = (num)=>{
//turn into string
if(Math.sign(num)===1)
return parseInt(num.toString().split('').reverse().join(''));
else return -1*parseInt(num.toString().split('').reverse().join(''));
}
console.log(revInt(-501));
回答by XP1
Here are reversible array functions in JavaScript that handle integers or strings:
以下是 JavaScript 中处理整数或字符串的可逆数组函数:
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
基准: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%.
两个函数之间的基准测试表明,在 Firefox 10 和 Chrome 12 中,字符串函数比整数函数快 30% 到 60%。在 Opera 12 中,整数函数稍微快了大约 10%。
回答by raksa eng
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));
回答by Romel jaudian
<html>
<script>
function reverseInt(n){
var r=0;
while(n!=0){
r*=10;
r+=n%10;
n=Math.floor(n/10);
}
return r;
}
</script>
</html>