如何获得 int (C#) 中的第一个数字?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/701322/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-04 14:02:10  来源:igfitidea点击:

How can you get the first digit in an int (C#)?

c#

提问by Dinah

In C#, what's the best way to get the 1st digit in an int? The method I came up with is to turn the int into a string, find the 1st char of the string, then turn it back to an int.

在 C# 中,获取 int 中第一个数字的最佳方法是什么?我想出的方法是将 int 转换为字符串,找到字符串的第一个字符,然后将其转换回 int。

int start = Convert.ToInt32(curr.ToString().Substring(0, 1));

While this does the job, it feels like there is probably a good, simple, math-based solution to such a problem. String manipulation feels clunky.

虽然这样做可以解决问题,但感觉可能有一个很好的、简单的、基于数学的解决方案来解决这样的问题。字符串操作感觉很笨重。

Edit:irrespective of speed differences, mystring[0] instead of Substring() is still just string manipulation

编辑:无论速度差异如何, mystring[0] 而不是 Substring() 仍然只是字符串操作

采纳答案by Anton Gogolev

Here's how

就是这样

int i = Math.Abs(386792);
while(i >= 10)
    i /= 10;

and iwill contain what you need

并且i将包含你所需要的

回答by Keltex

Very simple (and probably quite fast because it only involves comparisons and one division):

非常简单(而且可能相当快,因为​​它只涉及比较和一个除法):

if(i<10)
   firstdigit = i;
else if (i<100)
   firstdigit = i/10;
else if (i<1000)
   firstdigit = i/100;
else if (i<10000)
   firstdigit = i/1000;
else if (i<100000)
   firstdigit = i/10000;
else (etc... all the way up to 1000000000)

回答by jgauffin

int myNumber = 8383;
char firstDigit = myNumber.ToString()[0];
// char = '8'

回答by JaredPar

Try this

尝试这个

public int GetFirstDigit(int number) {
  if ( number < 10 ) {
    return number;
  }
  return GetFirstDigit ( (number - (number % 10)) / 10);
}

EDIT

编辑

Several people have requested the loop version

有几个人要求循环版本

public static int GetFirstDigitLoop(int number)
{
    while (number >= 10)
    {
        number = (number - (number % 10)) / 10;
    }
    return number;
}

回答by Lennaert

The best I can come up with is:

我能想到的最好的是:

int numberOfDigits = Convert.ToInt32(Math.Floor( Math.Log10( value ) ) );

int firstDigit = value / Math.Pow( 10, numberOfDigits );

...

...

Not very pretty :)

不是很漂亮:)

[Edited: first answer was really bad :) ]

[编辑:第一个答案真的很糟糕:)]

[Edit 2: I would probably advise the string manipulating solutions, though]

[编辑 2:不过,我可能会建议字符串操作解决方案]

[Edit 3: code formatting isnice :) ]

[编辑3:码格式好:)]

回答by Douglas Leeder

int temp = i;
while (temp >= 10)
{
    temp /= 10;
}

Result in temp

导致 temp

回答by JoshJordan

Just to give you an alternative, you could repeatedly divide the integer by 10, and then rollback one value once you reach zero. Since string operations are generally slow, this may be faster than string manipulation, but is by no means elegant.

只是给您一个替代方案,您可以重复将整数除以 10,然后在达到零时回滚一个值。由于字符串操作通常很慢,这可能比字符串操作快,但绝不优雅。

Something like this:

像这样的东西:

while(curr>=10)
     curr /= 10;

回答by cjk

while (i > 10)
{
   i = (Int32)Math.Floor((Decimal)i / 10);
}
// i is now the first int

回答by sdellysse

start = getFirstDigit(start);   
public int getFirstDigit(final int start){
    int number = Math.abs(start);
    while(number > 10){
        number /= 10;
    }
    return number;
}

or

或者

public int getFirstDigit(final int start){
  return getFirstDigit(Math.abs(start), true);
}
private int getFirstDigit(final int start, final boolean recurse){
  if(start < 10){
    return start;
  }
  return getFirstDigit(start / 10, recurse);
}

回答by MartinStettner

int start = curr;
while (start >= 10)
  start /= 10;

This is more efficient than a ToString() approach which internally must implement a similar loop and has to construct (and parse) a string object on the way ...

这比 ToString() 方法更有效,后者必须在内部实现类似的循环,并且必须在途中构造(并解析)一个字符串对象......