What are the options for formatting a string? What are the options for formatting when using .ToString()?

What are the options for formatting a string? What are the options for formatting when using .ToString()?

If you are using the .NET Framework you probably know that it was designed to make your life easy. Many modern programming languages are catching up with the idea and providing many pre-built features to facilitate the regular needs of developers. One of those needs is proper formatting of a text/string. Whenever we need to display information to the user, we can’t show five percent as 0.05 nor ten thousand dollars as 10000. Because of that need for formatting the .NET framework provides us with standard formatting tools. We will cover in this article some of them and explain to the reader how to use them to facilitate the development work.


Standard Numeric Format String

As you probably realize, you can format dates, addresses, but most importantly for us now Numbers. The Standard numeric format strings are used to format a number in some of the most familiar representations:

  • Currency
  • Decimal
  • Exponential (scientific)
  • Fixed-point
  • General
  • Number
  • Percent
  • Round-trip
  • Hexadecimal

In order to use this pre-configured formats, we are going to use the ToString method that has a format parameter, or you can also use it on methods such as Console.WriteLine and StringBuilder.AppendFormat. The other nice thing is that the format string takes the form A## where you can through the first part (“A”) specify the format and with the numeric portion (“##”) you can specify the precision. That way you can control how many digits in the string you want to show. It doesn’t perform any rounding, just truncates so take that into consideration and the accepted value range is from 0 to 99, with it using your culture/locale computer settings to determine the default value.

Below is a table obtained from MSDN with all the options for formatting using string.Format();. You can visit the website to obtain examples and additional information at (Standard numeric format strings):

Format specifier Name Description Examples
“C” or “c” Currency Result: A currency value.

Supported by: All numeric types.

Precision specifier: Number of decimal digits.

Default precision specifier: Defined by System.Globalization.NumberFormatInfo.

More information: The Currency (“C”) Format Specifier.

123.456 (“C”, en-US) -> $123.46

123.456 (“C”, fr-FR) -> 123,46 €

123.456 (“C”, ja-JP) -> ¥123

-123.456 (“C3”, en-US) -> ($123.456)

-123.456 (“C3”, fr-FR) -> -123,456 €

-123.456 (“C3”, ja-JP) -> -¥123.456

“D” or “d” Decimal Result: Integer digits with optional negative sign.

Supported by: Integral types only.

Precision specifier: Minimum number of digits.

Default precision specifier: Minimum number of digits required.

More information: The Decimal(“D”) Format Specifier.

1234 (“D”) -> 1234

-1234 (“D6”) -> -001234

“E” or “e” Exponential (scientific) Result: Exponential notation.

Supported by: All numeric types.

Precision specifier: Number of decimal digits.

Default precision specifier: 6.

More information: The Exponential (“E”) Format Specifier.

1052.0329112756 (“E”, en-US) -> 1.052033E+003

1052.0329112756 (“e”, fr-FR) -> 1,052033e+003

-1052.0329112756 (“e2”, en-US) -> -1.05e+003

-1052.0329112756 (“E2”, fr_FR) -> -1,05E+003

“F” or “f” Fixed-point Result: Integral and decimal digits with optional negative sign.

Supported by: All numeric types.

Precision specifier: Number of decimal digits.

Default precision specifier: Defined by System.Globalization.NumberFormatInfo.

More information: The Fixed-Point (“F”) Format Specifier.

1234.567 (“F”, en-US) -> 1234.57

1234.567 (“F”, de-DE) -> 1234,57

1234 (“F1”, en-US) -> 1234.0

1234 (“F1”, de-DE) -> 1234,0

-1234.56 (“F4”, en-US) -> -1234.5600

-1234.56 (“F4”, de-DE) -> -1234,5600

“G” or “g” General Result: The most compact of either fixed-point or scientific notation.

Supported by: All numeric types.

Precision specifier: Number of significant digits.

Default precision specifier: Depends on numeric type.

More information: The General (“G”) Format Specifier.

-123.456 (“G”, en-US) -> -123.456

123.456 (“G”, sv-SE) -> -123,456

123.4546 (“G4”, en-US) -> 123.5

123.4546 (“G4”, sv-SE) -> 123,5

-1.234567890e-25 (“G”, en-US) -> -1.23456789E-25

-1.234567890e-25 (“G”, sv-SE) -> -1,23456789E-25

“N” or “n” Number Result: Integral and decimal digits, group separators, and a decimal separator with optional negative sign.

Supported by: All numeric types.

Precision specifier: Desired number of decimal places.

Default precision specifier: Defined by System.Globalization.NumberFormatInfo.

More information: The Numeric (“N”) Format Specifier.

1234.567 (“N”, en-US) -> 1,234.57

1234.567 (“N”, ru-RU) -> 1 234,57

1234 (“N”, en-US) -> 1,234.0

1234 (“N”, ru-RU) -> 1 234,0

-1234.56 (“N”, en-US) -> -1,234.560

-1234.56 (“N”, ru-RU) -> -1 234,560

“P” or “p” Percent Result: Number multiplied by 100 and displayed with a percent symbol.

Supported by: All numeric types.

Precision specifier: Desired number of decimal places.

Default precision specifier: Defined by System.Globalization.NumberFormatInfo.

More information: The Percent (“P”) Format Specifier.

1 (“P”, en-US) -> 100.00 %

1 (“P”, fr-FR) -> 100,00 %

-0.39678 (“P1”, en-US) -> -39.7 %

-0.39678 (“P1”, fr-FR) -> -39,7 %

“R” or “r” Round-trip Result: A string that can round-trip to an identical number.

Supported by: SingleDouble, and BigInteger.

Precision specifier: Ignored.

More information: The Round-trip (“R”) Format Specifier.

123456789.12345678 (“R”) -> 123456789.12345678

-1234567890.12345678 (“R”) -> -1234567890.1234567

“X” or “x” Hexadecimal Result: A hexadecimal string.

Supported by: Integral types only.

Precision specifier: Number of digits in the result string.

More information: The HexaDecimal (“X”) Format Specifier.

255 (“X”) -> FF

-1 (“x”) -> ff

255 (“x4”) -> 00ff

-1 (“X4”) -> 00FF

Any other single character Unknown specifier Result: Throws a FormatException at run time.

Example:

using System;
using System.Globalization;
using System.Threading;

public class NumericFormats
{
   public static void Main()
   {
      // Display string representations of numbers for en-us culture
      CultureInfo ci = new CultureInfo("en-us");

      // Output floating point values
      double floating = 10761.937554;
      Console.WriteLine("C: {0}", 
              floating.ToString("C", ci));           // Displays "C: $10,761.94"
      Console.WriteLine("E: {0}", 
              floating.ToString("E03", ci));         // Displays "E: 1.076E+004"
      Console.WriteLine("F: {0}", 
              floating.ToString("F04", ci));         // Displays "F: 10761.9376"         
      Console.WriteLine("G: {0}",  
              floating.ToString("G", ci));           // Displays "G: 10761.937554"
      Console.WriteLine("N: {0}", 
              floating.ToString("N03", ci));         // Displays "N: 10,761.938"
      Console.WriteLine("P: {0}", 
              (floating/10000).ToString("P02", ci)); // Displays "P: 107.62 %"
      Console.WriteLine("R: {0}", 
              floating.ToString("R", ci));           // Displays "R: 10761.937554"            
      Console.WriteLine();

      // Output integral values
      int integral = 8395;
      Console.WriteLine("C: {0}", 
              integral.ToString("C", ci));           // Displays "C: $8,395.00"
      Console.WriteLine("D: {0}", 
              integral.ToString("D6", ci));          // Displays "D: 008395" 
      Console.WriteLine("E: {0}", 
              integral.ToString("E03", ci));         // Displays "E: 8.395E+003"
      Console.WriteLine("F: {0}", 
              integral.ToString("F01", ci));         // Displays "F: 8395.0"    
      Console.WriteLine("G: {0}",  
              integral.ToString("G", ci));           // Displays "G: 8395"
      Console.WriteLine("N: {0}", 
              integral.ToString("N01", ci));         // Displays "N: 8,395.0"
      Console.WriteLine("P: {0}", 
              (integral/10000.0).ToString("P02", ci)); // Displays "P: 83.95 %"
      Console.WriteLine("X: 0x{0}", 
              integral.ToString("X", ci));           // Displays "X: 0x20CB"
      Console.WriteLine();
   }
}

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.