Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# escape curly bracket not working with format modifier?

Tags:

c#

I know we can escape curly bracket in C# using {{ and }}. But they don't seem to work well if they are right after a format modifier (like {0:F6}).

string str;

// Prints "{3.14}" as expected
str = string.Format("{{{0}}}", 3.14);
Console.WriteLine(str);

// Expected "{3.140000}", found "{F6}"
str = string.Format("{{{0:F6}}}", 3.14);
Console.WriteLine(str);
like image 267
Chris Xue Avatar asked Dec 10 '15 17:12

Chris Xue


People also ask

What C is used for?

C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...

What is C full form?

Full form of C is “COMPILE”. One thing which was missing in C language was further added to C++ that is 'the concept of CLASSES'.

What language is C in?

C is a procedural language that provides no support for objects and classes. C++ is a combination of OOP and procedural programming languages. C has 32 keywords and C++ has 63 keywords. C supports built-in data types, while C++ supports both built-in and user-defined data types.

Is C language easy?

C is a general-purpose language that most programmers learn before moving on to more complex languages. From Unix and Windows to Tic Tac Toe and Photoshop, several of the most commonly used applications today have been built on C. It is easy to learn because: A simple syntax with only 32 keywords.


2 Answers

This is how C# processes curly brackets, it's well known. See here

You can escape it like this (for example, there are different ways):

 var str = string.Format("{0}{1:F6}{2}", "{", 3.14, "}");
 Console.WriteLine(str);
like image 120
blas3nik Avatar answered Oct 14 '22 05:10

blas3nik


Try this:

 String.Format("{0}{1:F6}{2}", "{",3.14, "}") 
like image 34
Akshey Bhat Avatar answered Oct 14 '22 04:10

Akshey Bhat