获得第七位的整数 [英] Get the seventh digit from an integer

查看:158
本文介绍了获得第七位的整数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个10位数的整数。我需要得到该整数的第7位。

我已经找到了数学解得到一个整数的第一位。

VAR myDigit = 2345346792; VAR firstDigit = Math.Abs​​(myDigit); 而(firstDigit> = 10) {      firstDigit / = 10; }

我怎样才能从myDigit第七位?我试图避免到字符串,并做一个子。我希望看到越来越第七位的mathemathical版本。

有人吗?

解决方案

  INT getSeventhDigit(INT数)
{
    而(数字> = 10000000)
        数/ = 10;

    返回数10%;
}
 

这将采取与或小于7位数字的最后一位。

对于具有8个或更多位的数字,它会除以10,直到数为7位数,则取最后一位。

I have a integer of 10 digits. I need to get the 7th digit of that integer.

I have found a mathematical solution to get the first digit of an integer.

var myDigit = 2345346792;

var firstDigit = Math.Abs(myDigit);
while (firstDigit >= 10)
{
     firstDigit /= 10;
}

How can I get the seventh digit from myDigit? I am trying to avoid casting to string and doing a substring. I would like to see the mathemathical version of getting the seventh digit.

Anyone?

解决方案

int getSeventhDigit(int number)
{
    while(number >= 10000000)
        number /= 10;

    return number % 10;
}

This will take the last digit of numbers with 7 or less digits.

For numbers with 8 or more digits, it will divide by 10 until the number is 7 digits long, then take the last digit.

这篇关于获得第七位的整数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆