我如何格式化数字与前导零的字符串? [英] How can I format a number into a string with leading zeros?

查看:131
本文介绍了我如何格式化数字与前导零的字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个数字,我需要转换为字符串。首先,我用这样的:

I have a number that I need to convert to a string. First I used this:

Key = i.ToString();

但我意识到这是一个陌生的顺序进行排序,所以我需要用零填充它。我怎么能这样做呢?

But I realize it's being sorted in a strange order and so I need to pad it with zeros. How could I do this?

推荐答案

相当简单:

Key = i.ToString("D2");

D 表示十进制数, 2 为数字要打印的份数。

D stands for "decimal number", 2 for the number of digits to print.

这篇关于我如何格式化数字与前导零的字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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