user2075764
user2075764

Reputation: 51

C# text formatting bug with braces

Why doesn't this code work correctly? Do do I misunderstand something?

System.Console.WriteLine("{{{0:c}}}", 12323.09m);

Real output:

{c}

Expected output:

{$12,323.09}

Upvotes: 1

Views: 68

Answers (2)

user700390
user700390

Reputation: 2339

Try this:

System.Console.WriteLine("{" + String.Format("{0:C}", 12323.09) + "}");

Upvotes: 0

AlexD
AlexD

Reputation: 32576

The issue is that {{{0:c}}} is parsed as {{ { ... }} }, and not as {{ { ... } }}.

Try

System.Console.WriteLine("{{{0:c}{1}", 12323.09m, '}');

Or see a similar sample in MSDN (see Escaping Braces):

int value = 6324;
string output = string.Format("{0}{1:D}{2}", 
                              "{", value, "}");
Console.WriteLine(output);

Upvotes: 3

Related Questions