简体   繁体   English

C#相当于这个VB6字符串格式?

[英]C# equivalent to this VB6 string formatting?

I'm converting an ancient VB6 program to C# and I came across some VB6 code that looked like this . 我正在将一个古老的VB6程序转换为C#,我遇到了一些看起来像这样的VB6代码。 . .

   Format(part(PI).far_xdev, " #0.000;-#0.000")

at first I was confused about the two format fields separated by a semicolon. 起初我对用分号分隔的两个格式字段感到困惑。 But it turns out that in VB6 this means that it uses the first one if the value of the number being formatted is 0 or positive, and the second if it's negative. 但事实证明,在VB6中,这意味着如果被格式化的数字的值为0或正数,则它使用第一个,如果是负数,则使用第二个。 If there were three formatting fields it would be positive, negative, and zero; 如果有三个格式化字段,则为正,负和零; four would be positive, negative, zero and null. 四个是正面,负面,零和零。

What's the equivalent of this in C# string formatting? 在C#字符串格式中,这相当于什么? How do I say "use this formating string for a positive number and that one for a negative number"? 我怎么说“将这个格式化字符串用于正数,而将其用于负数”?

(To whoever added the "This question may already have an answer": the problem with that link is that the linked question was asked in reference to some version of BASIC (based on the syntax) and did not explicitly say he was looking for an answer in C#, and neither of the two answers given specifically say they are in C#. We are left to surmise the languages involved based only on the tags. I think this new question and the resulting answers are much more clear, explicit and detailed) (对于任何添加“此问题可能已经有答案”的人:该链接的问题是链接问题是在参考某些版本的BASIC(基于语法)时提出的,并没有明确说明他正在寻找一个在C#中回答,并且两个答案都没有明确说明它们是在C#中。我们只能根据标签猜测所涉及的语言。我认为这个新问题和得到的答案更清晰,明确和详细)

Use the same separation by semi-column. 通过半列使用相同的分隔。 Read more about that separator at msdn (supports up to three sections) msdn上阅读有关该分隔符的更多信息(最多支持三个部分)

Console.WriteLine("{0:positive;negative;zero}", +1); //prints positive
Console.WriteLine("{0:positive;negative;zero}", -1); //prints negative
Console.WriteLine("{0:positive;negative;zero}", -0); //prints zero

You can use ToString on numeric value and pass format there 您可以在数值上使用ToString在那里传递格式

string formatted = 1.ToString("positive;negative;zero"); //will return "positive"

or use string.Format as shown in the comment section. 或者使用string.Format ,如注释部分所示。 But still you need to pass order position {0} to it. 但是你仍然需要将订单位置{0}传递给它。

string formatted = string.Format("{0:positive;negative;zero}", 1); 

In order to check for null, you can use null coalescing operator (cast to object is required, since there is no implicit cast from int? to string ). 为了检查null,可以使用null合并运算符(需要强制转换为对象,因为没有从int?string隐式转换)。 It becomes quite messy, so I would recommend to consider simple if statement. 它变得相当混乱,所以我建议考虑简单的if语句。

int? v = null;
var formatted = string.Format("{0:positive;negative;zero}", (object) v ?? "null");

C# supports the same custom formatting: C#支持相同的自定义格式:

string.Format("{0:#0.000;-#0.000}",part(PI).far_xdev);

Note that the format you use is the same as the standard fixed-point format with 3 significant digits: 请注意,您使用的格式与带有3位有效数字的标准定点格式相同:

string.Format("{0:F3}",part(PI).far_xdev);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM