How to Get the First Digit in an Int (C#)

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

Here's how

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

and i will contain what you need

Find first digit of a number using ONLY integer operations

Edit: Now tested for 0 and 100:

var result = n / 10 * (1 - n / 100) + n / 100 + (109 - n) / 100 * n;

How it works:


n | n / 10 * (1 - n / 100) | n / 100 | (109 - n) / 100 * n
-----------------------------------------------------------------
10 - 99 | 1 - 9 | 0 | 0
-----------------------------------------------------------------
100 | 0 | 1 | 0
-----------------------------------------------------------------
0 - 9 | 0 | 0 | 0 - 9

Getting the first digit of an int

@RayfenWindspear has the easiest to use and read answer, but is correct about the performance hit. If performance is more important than maintainability, you can do the same thing using iterative division to get the most-significant base-10 digit:

var i int
for i = n; i >= 10; i = i / 10 {}
// i == most significant digit

Note that you have to declare i outside the loop to be able to use it after the loop finds the most-significant digit. I'd also benchmark both with your own data set to see what the real performance impact is in your particular situation.

Full playground example, courtesy of Rayfen.

How do I test if the first digit of an integer is a 7?

You can test it before you parse the string like below:

if(!string.IsNullOrWhiteSpace(enteredValue)
&& enteredValue[0] == '7')
{
int.TryParse(enteredValue, out number);
}
else
{
Console.WriteLine("First digit was not 7");
Console.WriteLine(nRe-enter number ");
}

c# I need to get the first digit in a string and convert it to Morse code

Using Linq, first character is:

char firstDigit = this.Message.FirstOrDefault(c => char.IsDigit(c));

Then, create a Dictionary for converting a digit into Morse code.

class Program
{
static void Main(string[] args)
{
const string text = "abcde321x zz";
var morse = new Morse(text);
Console.WriteLine(morse.Code);
}
}

class Morse
{
private static Dictionary<char, string> Codes = new Dictionary<char, string>()
{
{'1', ".----"}, {'2', "..---"}, {'3', "...--"}, {'4', "....-"},
{'5', "....."}, {'6', "-...."}, {'7', "--..."}, {'8', "---.."},
{'9', "----."}, {'0', "-----"}
};
private string Message;
public string Code
{
get
{
char firstDigit = this.Message.FirstOrDefault(c => char.IsDigit(c));
return Codes.ContainsKey(firstDigit) ? Codes[firstDigit] : string.Empty;
}
}
public Morse(string message)
{
this.Message = message;
}
}

Output is:

...--

Get the first numbers from a string

The correct way to do this with Linq is as follows

number = new string(input.SkipWhile(c=>!char.IsDigit(c))
.TakeWhile(c=>char.IsDigit(c))
.ToArray());

Basically skip everything that isn't a digit, then stop taking characters when they are no longer digits. Note this would stop at punctuation, so it wouldn't pull something like "30.5" out of a string. If you need to deal with punctuation in the number then regular expressions would be the way to go. Also note that you don't need to do ToCharArray because string implements IEnumerable<char> which is all that is required for Linq.

Also you'll have to target .Net 4.0 as that is when they added the SkipWhile and TakeWhile extension methods.

Get first numbers from String

You can use the TakeWhile extension methods to get characters from the string as long as they are digits:

string input = "1567438absdg345";

string digits = new String(input.TakeWhile(Char.IsDigit).ToArray());

How to get the first and last digit a user entered in C#

Some people on here are a bit sensitive about helping with obvious homework problems. Half of the assignment is figuring it out.

Without trying to give too much, and still answering:

Try to treat it as a string. You can access a string like this:

var s = "this is a string";
Console.Write(s[0]); // t
Console.Write(s[1]); // h
Console.Write(s[2]); // i
...

Hopefully, you should be able to see what to do from here.



Related Topics



Leave a reply



Submit