开发者

Select a specific number in a number

开发者 https://www.devze.com 2023-03-24 03:14 出处:网络
I have a number say 123456. Each of these digits means something individually to me. I would like to be able to select say the fourth digit in this string of numbers (4) and assign it to variable.

I have a number say 123456. Each of these digits means something individually to me. I would like to be able to select say the fourth digit in this string of numbers (4) and assign it to variable.

M开发者_运维技巧y question is how would I go about doing this in mvc?


int number = 123456;

int fourth = number.ToString()[3];

Is this what you want?

Of course, it goes without saying that number has to have at least 4 digits.


Why not just convert it to a string and pull out the n-th character?

It'll be slower than some other methods, but I'm sure you and anyone else reading that piece of code will know exactly what it does.

Assuming you want to do this server-side:

int n = {the index of the digit you're intersted in};
int theNumber = 1233456;
char theDigit = theNumber.ToString()[n];

If you need to do this client-side in javascript you can do the following:

n = {the index of the digit you're interseted in};
theNumber = 123456;
digit = (theNumber + "")[n];
0

精彩评论

暂无评论...
验证码 换一张
取 消