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.
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.