C#:它的 String.Format 形式是什么?
如何将这个易于编写(和读取)的字符串格式化例程转换为“正确的”String.Format
等效代码?
Int32 power;
Single voltage;
Int32 kVA;
Double powerFactor;
powerFactor = power / kVA;
label1.Text =
DateTime.Now.ToString() + ": " +
power.ToString() + "W, " +
voltage.ToString() + "V "+
"(pf "+(powerFactor*100.0).ToString()+"%)";
//label1.Text = String.Format("{g}: {0:g}W, {0:g}V (p.f. {0:0%}%)",
// DateTime.Now, power, voltage, powerFactor);
我花了大约 10 分钟尝试使用 String.Format
;记录该事件的人应被解雇。
How can this easy to write (and read) string formatting routine be convert into the "proper" String.Format
equivalent code?
Int32 power;
Single voltage;
Int32 kVA;
Double powerFactor;
powerFactor = power / kVA;
label1.Text =
DateTime.Now.ToString() + ": " +
power.ToString() + "W, " +
voltage.ToString() + "V "+
"(pf "+(powerFactor*100.0).ToString()+"%)";
//label1.Text = String.Format("{g}: {0:g}W, {0:g}V (p.f. {0:0%}%)",
// DateTime.Now, power, voltage, powerFactor);
I've spent about 10 minutes trying to use String.Format
; the person who documented it should be terminated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
所以,这就是我认为让你感到困惑的事情。每个
{0}
都是您传入的对象的索引。{0}
是第一个对象,{1}
是第二个对象,等等。您还可以指定格式、宽度和其他太多的内容,无法在此处列出。我使用 SteveX string ref 来满足我的大部分需求。So, here is the thing that I think is confusing you. Every
{0}
is the index of the objects you are passing in.{0}
is the first object,{1}
the second, and so forth. You can also specify formats, widths, and other things too numerous to list here. I use SteveX string ref for most of my needs.