将 int 转换为具有一定长度 char 的字符串
如果标题不清楚,我会尽力在这里解释清楚。我有一堆整数,范围从 1 到 999,我需要将它们转换为字符串,但是当我这样做时,我需要它们的长度为 3 个字符。例如,如果我有:
int i1 = 45;
那么当我将其转换为字符串时,我需要这个:“045”
或者类似地,如果我有一个8的整数,那么它必须变成"008"
,如果任何东西有 3 个位置,例如 143,那么它只会输出为 143。这很容易实现吗?
感谢您提前回复。 :)
If the title wasn't clear, ill try to explain it well here. I have a bunch of integers, ranging from 1 to 999, and i need to convert these into strings, but when i do that, i need them to be 3 characters long. so for instance, if i had:
int i1 = 45;
then when i turned that into a string, i'd need this: "045"
or similarly, if i had an int of 8 then that would have to turn into "008"
, and if anything had 3 places, such as 143, then it would just be outputted as 143. is this easily possible?
Thanks for responses in advance. :)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果你想让它更有活力,你会做这样的事情
If you would like to make it more dynamic, you would do something like this