Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

String formatting with braces

I want to output a formatted number inside braces (example {$100.00}) using a string.Format(fmt, x) statement with x=100.

{      var x = 100M;     // works fine without a format specifier     string.Format("{{{0}}}", x);    // "{100.00}"      // fails with a format specifier     string.Format("{{{0:C}}}", x);  // "{C}"      // works with spaces     string.Format("{{ {0:C} }}", x);  // "{ $100.00 }" } 

So what format string should I use above to get {$100} without using a clunky solution such as string.Format("{0}{1:C}{2}", "{", x, "}");

like image 574
John Alexiou Avatar asked Aug 14 '14 15:08

John Alexiou


People also ask

How do you put curly braces in string format?

To escape curly braces and interpolate a string inside the String. format() method use triple curly braces {{{ }}} . Similarly, you can also use the c# string interpolation feature instead of String.

Which bracket is used for string formatting?

When we use f string formatting, we have to include 'f' before the opening quotation mark. Here, we can mention expression inside the curly brackets which will refer to variables or literal values.

How do you escape braces in string format?

Format strings contain “replacement fields” surrounded by curly braces {} . Anything that is not contained in braces is considered literal text, which is copied unchanged to the output. If you need to include a brace character in the literal text, it can be escaped by doubling: {{ and }} .

What do {} do in Python?

In languages like C curly braces ( {} ) are used to create program blocks used in flow control. In Python, curly braces are used to define a data structure called a dictionary (a key/value mapping), while white space indentation is used to define program blocks.


2 Answers

This is actually documented on MSDN under Composite Formatting:

The way escaped braces are interpreted can lead to unexpected results. For example, consider the format item "{{{0:D}}}", which is intended to display an opening brace, a numeric value formatted as a decimal number, and a closing brace. However, the format item is actually interpreted in the following manner:

  1. The first two opening braces ("{{") are escaped and yield one opening brace.
  2. The next three characters ("{0:") are interpreted as the start of a format item.
  3. The next character ("D") would be interpreted as the Decimal standard numeric format specifier, but the next two escaped braces ("}}") yield a single brace. Because the resulting string ("D}") is not a standard numeric format specifier, the resulting string is interpreted as a custom format string that means display the literal string "D}".
  4. The last brace ("}") is interpreted as the end of the format item.
  5. The final result that is displayed is the literal string, "{D}". The numeric value that was to be formatted is not displayed.

One way to write your code to avoid misinterpreting escaped braces and format items is to format the braces and format item separately. That is, in the first format operation display a literal opening brace, in the next operation display the result of the format item, then in the final operation display a literal closing brace. The following example illustrates this approach.

int value = 6324; string output = string.Format("{0}{1:D}{2}",                               "{", value, "}"); Console.WriteLine(output); // The example displays the following output:  //       {6324} 

(note that I add the code only because it's referenced in the MSDN article - not to suggest it as a solution.)

If you're not concerned about culture differences you could use "{{{0:$#,##0.00}}}" as a format string.

like image 134
D Stanley Avatar answered Sep 26 '22 01:09

D Stanley


Try to use this code:

string.Format("{{{0}}}", x.ToString("C")) 
like image 31
Norfolc Avatar answered Sep 26 '22 01:09

Norfolc