如何将前导零添加到新的C#插值字符串? [英] How do I add leading zeroes to the new C# interpolated strings?

查看:89
本文介绍了如何将前导零添加到新的C#插值字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述





我正在努力解决如何在新的C#插值字符串中填充前导零的整数字段。



根据本文 C#6:String Interpolation ,相同的复合格式化功能可用于指定重要数字和前导零等内容。



在同一篇文章中是此页面的链接:标准数字格式字符串 [ ^ ]。根据这个页面,类似D2的东西应该使用两个数字作为整数并使用零填充。当我尝试它时,似乎什么都不做。



例如



Hi,

I am struggling to work out how to pad an integer field with leading zeroes in the new C# interpolated strings.

According to this article C#6: String Interpolation, "The same composite formatting power is there to specify things like significant figures and leading zeroes."

In this same article is a link to this page: Standard Numeric Format Strings[^]. According to this page, something like "D2" should use two digits for an integer and pad with zeroes. When I try it, it seems to do nothing.

E.g.

int x = 3;
Console.WriteLine( $"Integer padded to two places: {x:D2}." );



打印3,而不是03。



有谁知道如何这样做吗?



亲切的愿望~Patrick



PS - 从下面的讨论看,似乎我有没有100%清楚我想要实现的目标。



我知道数字总是在0 <= x <= 99的范围内。我得到0-9范围内的输入,我希望它添加一个前导零(例如4个打印04,但14个打印14)。希望这澄清了这个问题。



我尝试过:



除了上面显示的代码之外,我还尝试过##,DD和其他一些我忘记的变种。


This prints "3", not "03".

Does anyone know how to do this?

Kind wishes ~ Patrick

PS - Judging by discussion below, it seems I have not been 100% clear about what I am trying to achieve.

I know the numbers will always be in the range 0 <= x <= 99. When I get an input in the range 0-9, I want it to add one leading zero (e.g. 4 prints "04", but 14 prints "14"). Hope this clarifies the question.

What I have tried:

In addition to the code shown above, I have tried "##", "DD" and a couple of other variants that I have forgotten.

推荐答案

整数填充到两个位置:{x:D2}。);



打印3,而不是03。



有谁知道怎么做?

< b / b
亲切的愿望~Patrick



PS - 从下面的讨论来看,似乎我没有100%清楚我想要实现的目标。



我知道数字总是在0< = x< = 99的范围内。当我得到0-9范围内的输入时,我希望它添加一个前导零(例如4个打印04,但14个打印14)。希望这澄清了这个问题。



我尝试过:



除了上面显示的代码之外,我还尝试了##,DD和其他一些我忘记的变体。


This prints "3", not "03".

Does anyone know how to do this?

Kind wishes ~ Patrick

PS - Judging by discussion below, it seems I have not been 100% clear about what I am trying to achieve.

I know the numbers will always be in the range 0 <= x <= 99. When I get an input in the range 0-9, I want it to add one leading zero (e.g. 4 prints "04", but 14 prints "14"). Hope this clarifies the question.

What I have tried:

In addition to the code shown above, I have tried "##", "DD" and a couple of other variants that I have forgotten.


不完全是C#6,但很简单:

Not exactly C# 6, but simple:
string x = "3";
Console.WriteLine(x.PadLeft(2, '0'))


D或d在标准中的说明符中数字格式字符串不是它。它不是前导/填充,这是精确说明符:最小位数。你能看到区别么?以下是另一种解释:标准数字格式字符串 [ ^ ]。



对于前导数字或填充,您需要自定义数字格式字符串:自定义数字格式字符串 [ ^ ]。



注意第一行,说明符0,另见#。玩一下;你会很快看到它的真正含义。



-SA
"D" or "d" in specifier found in "Standard Numeric Format Strings" is not it. It is not leading/padding, this is "Precision specifier: Minimum number of digits". Can you see the difference? Here is an additional explanation: Standard Numeric Format Strings[^].

For leading digits or padding, you need "Custom Numeric Format Strings": Custom Numeric Format Strings[^].

Pay attention for the first line, the specifier "0", see also "#". Play with it a bit; and you will quickly see what it really means.

—SA


这篇关于如何将前导零添加到新的C#插值字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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