C# - 增量数量,并保持在前面的零 [英] C# - increment number and keep zeros in front

查看:195
本文介绍了C# - 增量数量,并保持在前面的零的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要一个40位的计数器变量。它的开头应为 0000000000000000000000000000000000000001 结果
和增量结果
0000000000000000000000000000000000000002

I need to make a 40 digit counter variable. It should begin as 0000000000000000000000000000000000000001
and increment to
0000000000000000000000000000000000000002

当我使用 INT 类,它切断所有的零。问题是,我需要增加数量,然后将其转换为字符串,正确数量的前导零。总大小应为40位数字。所以,如果我打50为例,它应该是这样的:

When I use the int class, it cuts off all the zeros. Problem is I need to increment the number and then convert it to a string, with the correct number of leading zeros. The total size should be 40 digits. So if I hit 50 for example, it should look like this:

0000000000000000000000000000000000000050

我怎么能做到这一点,并保留了零?

How can I do that and retain the zeros?

推荐答案

使用的整数格式或垫当你转换为字符串的结果。如

Use the integer and format or pad the result when you convert to a string. Such as

int i = 1;
string s = i.ToString().PadLeft(40, '0');

请参阅叶普斯蒂格尼尔森的回答了解,我也永远记得格式化选项。

See Jeppe Stig Nielson's answer for a formatting option that I can also never remember.

这篇关于C# - 增量数量,并保持在前面的零的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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