在 C# 中,我有一个整数值需要转换为字符串,但它需要在之前添加零:
In C# I have an integer value which need to be convereted to string but it needs to add zeros before:
例如:
int i = 1;
当我将它转换为字符串时,它需要变成 0001
When I convert it to string it needs to become 0001
我需要知道 C# 的语法.
I need to know the syntax in C#.
i.ToString().PadLeft(4, '0')
- 好的,但不适用于负数i.ToString("0000");
- 显式形式i.ToString("D4");
- 短格式说明符$"{i:0000}";
- 字符串插值 (C# 6.0+)
i.ToString().PadLeft(4, '0')
- okay, but doesn't work for negative numbers
i.ToString("0000");
- explicit form
i.ToString("D4");
- short form format specifier
$"{i:0000}";
- string interpolation (C# 6.0+)
这篇关于C#将int转换为带有填充零的字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!