打印long long int值 [英] Printing long long int values

查看:194
本文介绍了打印long long int值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述





我有一个18个字符的整数值,我声明为long long int,比如long long int license。许可证长度实际上应该是20个字符(在我的情况下有效许可证总是20个字符),所以为方便起见,我将00附加到许可证和输入。



打印时,它缺少领先的00。如何在输出中打印前导00?



我尝试过:



例如:

long long int license;

我如何传递它作为输入= 00123456789876543219

预期o / p: 00123456789876543219

实际输出:123456789876543219

Hi,

I have an integer value of 18 characters, which i declare as a long long int, say long long int license. The license length should be actually 20 characters( a valid license in my case will always be 20 characters), so for convenience, I append 00 to the license and input.

While printing, it is missing the leading 00's. How can I print the leading 00 also in the output?

What I have tried:

eg:
long long int license;
how i pass it as input =00123456789876543219
expected o/p: 00123456789876543219
actual output:123456789876543219

推荐答案

我建​​议将您的许可证存储为字符串,因为这就是您对待它的方式。您没有提到需要对该数字进行数值计算,因此不需要转换该字符串。然后存储和打印应该是微不足道的。
I would suggest to store your license as a string, because that's how you treat it. You haven't mentioned any need to do numerical calculations on that number, so there should be no need to ever convert that string. Storing and printing then should be trivial.


使用sprintf将其格式化为前导零。
use sprintf to format it with leading zeros.


Mr。西蒙斯走在正确的轨道上也是k5054。我用它的一个变体来做:
Mr. Simmons is on the right track and k5054 also. I use a variant of that to do it :
sprintf( outputBuffer, "%020I64d", sixtyfourbitvalue );

这将是带有前导零的二十位数。



顺便说一句,在stdint.h中它有这个定义:

That will be twenty digits with leading zeros.

Incidentally, in stdint.h it has this definition :

#define INT64_MAX        9223372036854775807i64

所以您可能想尝试类似的格式。也就是说,如果您使用的是visual studio。这些不是64位值的标准格式。



为了一个有趣的练习,尝试编写一个函数来接受64位整数并加载一个文本缓冲区逗号分隔的字符串。例如:1,234,456,890。根据您的位置,您可以使用点来分隔数千个,以便奖励积分使用区域设置来选择适当的分隔字符。

so you might want to try a similar format. That is, if you are using visual studio. Those are not standard formats for 64-bit values.

For a fun exercise, try writing a function to accept a 64-bit integer and load a text buffer with a comma-separated string. For example: 1,234,456,890. Depending on your location, you might use dots for separating the thousands so for bonus credit use the locale to select the appropriate separation character.


这篇关于打印long long int值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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