• Status: Solved
• Priority: Medium
• Security: Public
• Views: 3043

# Find the last digit in the number using c# ?

i need to find last digit in the number ?
if it is an integer there is no need to check
0
Varshini
• 3
• 2
• 2
• +2
1 Solution

Billing EngineerCommented:
is that inside a string?
no=> put it into a string
yes=>
``````string number = "25.78";
string digit = ( number.IndexOf(".") >= 0 ? number.Substring(number.Length,1) : "0" );
``````
0

Commented:
convert the number to String and take the last char, then convert it to integer again.

How to get the last char from a String? you know string.length and you can take charAt(index i).

0

Author Commented:
angelIII:
i got following error when i excute the code ...

Index and length must refer to a location within the string.
Parameter name: length
0

Billing EngineerCommented:
sorry, substring is 0-based
``````string number = "25.78";
string digit = ( number.IndexOf(".") >= 0 ? number.Substring(number.Length-1,1) : "0" );
``````
0

Commented:
What about 25.7?  What should be the result?
0

Commented:
What about 25.70104? What should be the result?
0

Commented:
OK, I put together number of cases:
double d1 = 25.78d;
double d2 = 25d;
double d3 = 25.7d;
double d4 = 25.784d;
double d5 = 25.786d;

Output: (last digit-formatted string with two chars, initial number)

8      25.78      25.78
0      25.00      25
0      25.70      25.7
8      25.78      25.784
9      25.79      25.786

I believe it's what's required :)

See the code
``````double d1 = 25.78d;
double d2 = 25d;
double d3 = 25.7d;
double d4 = 25.784d;
double d5 = 25.786d;

string s1 = string.Format("{0:F2}", d1);
string s2 = string.Format("{0:F2}", d2);
string s3 = string.Format("{0:F2}", d3);
string s4 = string.Format("{0:F2}", d4);
string s5 = string.Format("{0:F2}", d5);

string lastCh1 = s1.Substring(s1.Length - 1, 1);
string lastCh2 = s2.Substring(s2.Length - 1, 1);
string lastCh3 = s3.Substring(s3.Length - 1, 1);
string lastCh4 = s4.Substring(s4.Length - 1, 1);
string lastCh5 = s5.Substring(s5.Length - 1, 1);

string result = "";

result = result + lastCh1 + "\t" + s1 + "\t" + d1.ToString() + Environment.NewLine;
result = result + lastCh2 + "\t" + s2 + "\t" + d2.ToString() + Environment.NewLine;
result = result + lastCh3 + "\t" + s3 + "\t" + d3.ToString() + Environment.NewLine;
result = result + lastCh4 + "\t" + s4 + "\t" + d4.ToString() + Environment.NewLine;
result = result + lastCh5 + "\t" + s5 + "\t" + d5.ToString() + Environment.NewLine;

System.Diagnostics.Debug.WriteLine(result);
``````
0

Commented:
Here is modified code where getting last digit moved to a separate method - two lines:

private string lastChar(double d)
{
string s = string.Format("{0:F2}", d);
return s.Substring(s.Length - 1, 1);
}
``````private void button1_Click(object sender, EventArgs e)
{
double d1 = 25.78d;
double d2 = 25d;
double d3 = 25.7d;
double d4 = 25.784d;
double d5 = 25.786d;
double d6 = 0d;

string lastCh1 = lastChar(d1);
string lastCh2 = lastChar(d2);
string lastCh3 = lastChar(d3);
string lastCh4 = lastChar(d4);
string lastCh5 = lastChar(d5);
string lastCh6 = lastChar(d6);

string result = "";

result = result + lastCh1 + "\t" +d1.ToString() + Environment.NewLine;
result = result + lastCh2 + "\t" + d2.ToString() + Environment.NewLine;
result = result + lastCh3 + "\t" + d3.ToString() + Environment.NewLine;
result = result + lastCh4 + "\t" + d4.ToString() + Environment.NewLine;
result = result + lastCh5 + "\t" + d5.ToString() + Environment.NewLine;
result = result + lastCh6 + "\t" + d6.ToString() + Environment.NewLine;

System.Diagnostics.Debug.WriteLine(result);
}

private string lastChar(double d)
{
string s = string.Format("{0:F2}", d);
return s.Substring(s.Length - 1, 1);
}
``````
0

Commented:
Here is an alternative approach using math:

GetDecimalDigit(25.78, 2)  -  returns 8
GetDecimalDigit(25, 2)       -  returns 0
GetDecimalDigit(25.69, 2)  -  returns 9

``````public static int GetDecimalDigit(double d, int decimalPosition)
{
int pow = (int)Math.Pow(10, decimalPosition);

return  (int)(d * pow) % (pow / 10);
}
``````
0

## Featured Post

• 3
• 2
• 2
• +2
Tackle projects and never again get stuck behind a technical roadblock.