Link to home
Start Free TrialLog in
Avatar of Varshini
Varshini

asked on

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
25.78  answer 8
25     answer  0
25.69  answer 9
Avatar of Guy Hengel [angelIII / a3]
Guy Hengel [angelIII / a3]
Flag of Luxembourg image

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" );

Open in new window

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).

Avatar of Varshini
Varshini

ASKER

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

Index and length must refer to a location within the string.
Parameter name: length
sorry, substring is 0-based
string number = "25.78";
string digit = ( number.IndexOf(".") >= 0 ? number.Substring(number.Length-1,1) : "0" );

Open in new window

What about 25.7?  What should be the result?
What about 25.70104? What should be the result?
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);

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Dmitry G
Dmitry G
Flag of New Zealand image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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);
}

Open in new window