C#:它的 String.Format 形式是什么?

发布于 2024-09-26 13:39:12 字数 544 浏览 0 评论 0原文

如何将这个易于编写(和读取)的字符串格式化例程转换为“正确的”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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

玩世 2024-10-03 13:39:12
string.Format("{0}: {1}W,  {2}V (pf {3}%",DateTime.Now,power,voltage,powerFactor*100)

所以,这就是我认为让你感到困惑的事情。每个 {0} 都是您传入的对象的索引。{0} 是第一个对象,{1} 是第二个对象,等等。您还可以指定格式、宽度和其他太多的内容,无法在此处列出。我使用 SteveX string ref 来满足我的大部分需求。

label1.Text = String.Format("{0:g}: {1:g}W, {2:g}V (p.f. {3:0.0%})", 
      DateTime.Now, power, voltage, powerFactor);
string.Format("{0}: {1}W,  {2}V (pf {3}%",DateTime.Now,power,voltage,powerFactor*100)

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.

label1.Text = String.Format("{0:g}: {1:g}W, {2:g}V (p.f. {3:0.0%})", 
      DateTime.Now, power, voltage, powerFactor);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文