开发者

C#: What's the String.Format form of this?

开发者 https://www.devze.com 2023-01-19 04:38 出处:网络
How can this easy to write (and read) string formatting routine be convert into the \"proper\" String.Format equivalent code?

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开发者_JAVA技巧; the person who documented it should be terminated.


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);
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号