如何截掉前导数字? C++
如何截掉数字的前导数字,以便仅显示最后两位数字,而不使用库。例如:
1923 至 23
2001 至 01
1234 至 34
123 至 23
仅包含
#include <iomanip>
#include <iostream>
感谢!
How can I cut off the leading digits of a number in order to only display the last two digits, without using the library. For example:
1923 to 23
2001 to 01
1234 to 34
123 to 23
with only
#include <iomanip>
#include <iostream>
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您只是使用整数,为了简单起见,我建议只执行 mod %100:
将显示 41。
如果您需要前导零,只需执行以下操作:
If you're just working with integers I'd suggest just doing mod %100 for simplicity:
Would display 41.
And if you need a leading zero just do:
如果您的数字是 int 形式(而不是字符串形式),您应该考虑使用 取模运算符。
如果数字采用 char[] 形式,则有一个简单的解决方案,涉及对字符串进行索引,例如:
If your numbers are in int form (rather than string form), you should think about using the modulo operator.
If the numbers are in char[] form, there is an easy solution that involves indexing into the string, like: