以'n'形式打印,其中n由用户I'C'输入 [英] Printing in form of 'n' digits where n is entered by user I 'C'

查看:130
本文介绍了以'n'形式打印,其中n由用户I'C'输入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将int转换为字符串,但必须以n位数的形式完成。

即如果用户想要5位数空间则为5,然后是00005,如果是6位数空间000005.

我知道可以用%05d等形式完成,但这里的值是由用户输入的,所以%0nd不会工作。我怎么能这样做。

I want to convert an int to a string but that has to be done in form of n digits.
i.e for 5 if user wants it in 5 digit space then 00005 , if in 6 digit space 000005.
I know that can be done in form of %05d etc but here the value is entered by the user so %0nd wont work . How can I do that.

sprintf(sol,"%nd",i);

如何纠正?

提前致谢



我的尝试:



原始的想法是使用

How can this be corrected?
Thanks in advance

What I have tried:

The primitive thought was to use

%nd

但当然这不起作用。

推荐答案

神奇的咒语是%* d作为格式说明符。然后,您提供另一个参数(整数)来指定宽度。请参阅printf格式说明符的任何好参考。

(编辑)您还需要指定前导零填充。你会在理智的参考文献中找到它。
The magic incantation is "%*d" as a format specifier. You then provide another argument (integer) to specify the width. See any good reference on printf format specifiers.
(edit) you'll also need to specify leading zero padding. you'll find that in the sane reference.


这篇关于以'n'形式打印,其中n由用户I'C'输入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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