English 中文(简体)
A. 激励分类
原标题:String Formatting for Integers c#

我有一个包含两条插图的班子,一个反映本年度,一个具有一定价值。 这些领域是根据用户具体规定的格式(使用方形)合并的。 IMPORTANT:用户输入数据,用于生成数据,因此,数据总是一种愤怒,我们确实对此感到担忧。

我们的缺省格式是“{0}-{0}”。 然而,现在用户具体数据并不希望适当格式。 例如:

用户输入12个,因为需要数据。 在编排格式时,而不是显示2011-0012,而是显示2011-12年。 我怎么能确保,如果不做一些cra,那就能够补充零 s,或试图将数字 par平(假设这是数)。 仅应有0分之四的长度?

这是我尝试的一种形式:

"{0}-{1:0000}" -> the original format when the user was forced to enter numbers. "{0}-{1:D4}" "{0}-{1:N4}"

最佳回答

您可使用string.PadLeft(:

string output = string.Format("{0}-{1}", "2011", input.PadLeft(4,  0 ));
问题回答

您可使用<代码>。 PadLeft,在左边加零,或使用int。 TryParse 试图转换为一种愤怒。 后者将翻一番,作为你的验证检查。





相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签