Get last digit of a number

Good morning, I have a question where the interviewer enters a number,
for example: number 34158, I need to get the digit 8.
How can I get the last digit of that number,

Thanks for your directions.

1 Like

Assuming your number is a NUMERIC Question, let’s call it q_number, I think there are at least two ways:

  1. Create a new variable (Long Integer) lastdigit_int with expression: Math.Abs((int)q_number% 10)
    This will return the absolute value of the last digit. If you are fine with negative values just use ((int)q_number% 10)
    If you use decimals, it will only return the last digit of the whole part.
  2. Create a new variable (string) lastdigit_string with expression: Convert.ToString(q_number).Substring(Convert.ToString(q_number).Length - 1)
    This will return the last value, the last value of the fractional part if decimal is used, as a string.
    If you need the output as a numeric value to use it in enabling or validation conditions you could create an additional variable lastdigit_string_integer with expression: Convert.ToInt32(lastdigit_string) or add the expression of lastdigit_string into Convert.ToInt32(…)

I guess there are more ways to do it…


Thanks Peter it works. If the number is a TEXT question, what would be the expression I should use?

Try this:


Dear all,

I have a numeric question about version of questionnaire where the interviewer enters a number of 6 digits, for example: 229001.
How I can get the first digit of this number? I mean I need to get digit 2.

Many thanks for your help…

Yo wrote that it aims to capture “version of questionnaire” which sounds like to enter a unique id or identifier of interview. Try to avoid this scenario and switch to assignments using identifying questions.

To answer your question:

First digit of Numeric Question ( num_q):

If you want to store the result as a string create a variable of type string with expression:

> Convert.ToString(num_q).Left(1)

Two last digits of Numeric Question ( num_q):

> Convert.ToString(num_q).Right(2)