如何扭转一个数为整数,而不是作为一个字符串?作为一个、整数、字符串、数为

2023-09-10 23:30:36 作者:旧容颜

我遇到了一个问题:怎样才能扭转数为整数,而不是作为一个字符串?任何人都可以请帮我找出答案。

I came across a question "How can one reverse a number as an integer and not as a string?" Could anyone please help me to find out the answer.

推荐答案

这应该做到这一点:

int n = 12345;
int left = n;
int rev = 0;
while(left>0)
{
   r = left % 10;   
   rev = rev * 10 + r;
   left = left / 10;  //left = Math.floor(left / 10); 
}

Console.WriteLine(rev);