Apr 17, 2014

[How to] Output left brace "{" in String.Format

It's very simple, just using left brace double times "{{" to output a normal left brace.

The following example is that converting a 3D point to string.


public string ToString(int d){
  if (d < 0)
    d = 0;
  else if (d > 12)
    d = 12;

  string format = string.Format("({{0:N{0}}}, {{1:N{0}}}, {{2:N{0}}})", d, d, d);
  return string.Format(format, X, Y, Z);
}