如何读取整数的一部分 [英] How to read a part of an integer

查看:92
本文介绍了如何读取整数的一部分的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




我需要通过另一个变量读取整数的一部分。

例如,

如果用户输入761120921,

然后,i = 761120921

j = 76

k = 112

l = 0921。

你能解释我怎么做到这一点吗?

解决方案

panther写道:





我需要通过另一个变量读取整数的一部分。

例如,

如果用户输入761120921,

那么,i = 761120921

j = 76

k = 112

l = 0921那样。

你能解释我怎么实现这个?



你需要手动分割数字。首先,你需要0921或921

作为最后一个号码吗?因为如果你不想放弃零,你就可以在字符串级别工作
。如果你想要放弃零,而你只是想要通常用整数工作,你可以通过
取得

的最后四位数。

i%10000;


然后去高位数字


i / = 10000;


问候,


Zeppe


5月3日下午12:53,豹< tharaka ... @ gmail.comwrote:





我需要读取整数的一部分另一个变量。

例如,

如果用户输入761120921,

那么,i = 761120921

j = 76

k = 112

l = 0921那个。

你能解释我怎么做到这一点?



豹的反应是正确的方法。

但是,如果你只需要将整数转换为字符串,你

可以使用itoa(int)或其他一些类似的功能。


panther< th **** **** @ gmail.com写道:


我需要通过另一个变量读取整数的一部分。

例如,

如果用户输入761120921,

那么,i = 761120921

j = 76

k = 112

l = 0921那个。

你能解释我怎么做到这一点?



一种方法是使用%和/运算符。例如,


i = 761120921

j = i / 10000000

k =(i / 10000)%1000

l = i%1000

另一种方法是以字符串形式读取数字,并使用

string :: substr()函数。


-

Marcus Kwok

用''net''替换''invalid''回复


Hi,

I need to read part of an integer by another variable.
As example,
if user enter 761120921,
then, i = 761120921
j = 76
k = 112
l = 0921 as that.
can you explain how do i acheive this?

解决方案

panther wrote:

Hi,

I need to read part of an integer by another variable.
As example,
if user enter 761120921,
then, i = 761120921
j = 76
k = 112
l = 0921 as that.
can you explain how do i acheive this?

you need to split the number manually. At first, do you need 0921 or 921
as a last number? because if you don''t want to loose the zero you have
to work at a string level. If you want to loose the zero, and you just
want to work generally with integers, you can just for example take the
last four digits by doing

i % 10000;

and then go for the high order digits by

i /= 10000;

Regards,

Zeppe


On May 3, 12:53 pm, panther <tharaka...@gmail.comwrote:

Hi,

I need to read part of an integer by another variable.
As example,
if user enter 761120921,
then, i = 761120921
j = 76
k = 112
l = 0921 as that.
can you explain how do i acheive this?

panther''s response is the right way to do it.
However, if you simply need to turn the integer into a string, you
could use itoa(int) or a some other comparable functions.


panther <th********@gmail.comwrote:

I need to read part of an integer by another variable.
As example,
if user enter 761120921,
then, i = 761120921
j = 76
k = 112
l = 0921 as that.
can you explain how do i acheive this?

One way would be to use the % and / operators. For example,

i = 761120921
j = i / 10000000
k = (i / 10000) % 1000
l = i % 1000

Another way would be to read the number in as a string, and use the
string::substr() function.

--
Marcus Kwok
Replace ''invalid'' with ''net'' to reply


这篇关于如何读取整数的一部分的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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