c# - 格式化 C# 输出

我正在尝试格式化一些 c# 输出,以便数字始终有 2 位数字,例如如果 int = 0 我希望 Console.WriteLine(int); 到产生 00。

最佳答案

看看http://msdn.microsoft.com/en-us/library/0c899ak8.aspx

这应该做你想做的:

        int num = 10;

        Console.WriteLine(num.ToString("0#"));

        Console.ReadLine();

传递给ToString方法的字符串“0#”的含义如下:

0 - 0 place holder, this will be replaced with relevant digit if one exists
# - digit place holder.

所以如果 num 为 0,00 将被写入控制台,但如果 num 为 10,则 10 将被写入控制台。

https://stackoverflow.com/questions/2415627/

相关文章:

python - 从 lxml.objectify.ObjectifiedElement 中剥离 p

file-io - 将数据按列写入文件(Fortran)

postgresql - 在 PostgreSQL 中格式化 double

django - 如何格式化 django 评论的表单字段?

.net - 如何将 NLog 的文件名设置为进程开始日期?

arrays - Bash 数组 : appending and prepending to eac

mysql - 这是什么时间格式? (不是 UNIX,不是 UTC,什么都没有)

c# - 使用 C# 格式化字符串中的句子

java - 将 4 位军用时间转换为标准 12 小时时间格式

c# - 将规范化的电话号码转换为用户友好的版本