RSS

Standard numeric format string quick reference

16 May

The look-up table below is implemented by ToString method with “en-US” CultureInfo. More detailed examples could be found at Standard Numeric Format Strings Output Examples.

Format
Specifier
Format
Name
Format
String
Output
(int 1234)
Output
(double 12345.6789)
C or c Currency C $1,234.00 $12,345.68
D or d Decimal D 1234 Format specifier
was invalid.
E or e Scientific E 1.234000E+003 1.234568E+004
F or f Fixed-point F 1234.00 12345.68
G or g General G 1234 12345.6789
N or n Number N 1,234.00 12,345.68
P or p Percentage P 123,400.00 % 1,234,567.89 %
R or r Round-trip R Format specifier
was invalid.
12345.6789
X or x Hexadecimal X 4D2 Format specifier
was invalid.

Change CultureInfo in the code to see different output results.

public partial class StandardNumericFormat : Form
{
    public StandardNumericFormat()
    {
        InitializeComponent();
    }

    private void StandardNumericFormat_Load(object sender, EventArgs e)
    {
        int testInt = 1234;
        double testDouble = 12345.6789;
        CultureInfo usCulture = new CultureInfo("en-US");
        CultureInfo frCulture = new CultureInfo("fr-FR");

        DataTable dt = new DataTable();
        dt.Columns.Add("Specifier");
        dt.Columns.Add("FormatName");
        dt.Columns.Add("Format");
        dt.Columns.Add("Culture");
        dt.Columns.Add("Int");
        dt.Columns.Add("Double");

        // en-US
        this.AddNewRow(dt, testInt, testDouble, usCulture, "C or c", "Currency", "C");
        this.AddNewRow(dt, testInt, testDouble, usCulture, "D or d", "Decimal", "D");
        //this.AddNewRow(dt, testInt, testDouble, usCulture, "D or d", "Decimal", "D8");
        this.AddNewRow(dt, testInt, testDouble, usCulture, "E or e", "Scientific", "E");
        //this.AddNewRow(dt, testInt, testDouble, usCulture, "E or e", "Scientific", "e");
        //this.AddNewRow(dt, testInt, testDouble, usCulture, "E or e", "Scientific", "E10");
        this.AddNewRow(dt, testInt, testDouble, usCulture, "F or f", "Fixed-point", "F");
        //this.AddNewRow(dt, testInt, testDouble, usCulture, "F or f", "Fixed-point", "F0");
        //this.AddNewRow(dt, testInt, testDouble, usCulture, "F or f", "Fixed-point", "F6");
        this.AddNewRow(dt, testInt, testDouble, usCulture, "G or g", "General", "G");
        this.AddNewRow(dt, testInt, testDouble, usCulture, "N or n", "Number", "N");
        this.AddNewRow(dt, testInt, testDouble, usCulture, "P or p", "Percentage", "P");
        this.AddNewRow(dt, testInt, testDouble, usCulture, "R or r", "Round-trip", "R");
        this.AddNewRow(dt, testInt, testDouble, usCulture, "X or x", "Hexadecimal", "X");

        // fr-FR
        //this.AddNewRow(dt, testInt, testDouble, frCulture, "C or c", "Currency", "C");
        //this.AddNewRow(dt, testInt, testDouble, frCulture, "D or d", "Decimal", "D");
        //this.AddNewRow(dt, testInt, testDouble, frCulture, "E or e", "Scientific", "E");
        //this.AddNewRow(dt, testInt, testDouble, frCulture, "F or f", "Fixed-point", "F");
        //this.AddNewRow(dt, testInt, testDouble, frCulture, "G or g", "General", "G");
        //this.AddNewRow(dt, testInt, testDouble, frCulture, "N or n", "Number", "N");
        //this.AddNewRow(dt, testInt, testDouble, frCulture, "P or p", "Percentage", "P");
        //this.AddNewRow(dt, testInt, testDouble, frCulture, "R or r", "Round-trip", "R");
        //this.AddNewRow(dt, testInt, testDouble, frCulture, "X or x", "Hexadecimal", "X");

        this.dataGridView1.DataSource = dt;
    }

    private void AddNewRow(DataTable dt, int testInt, double testDouble, CultureInfo testCulture,
        string specifier, string name, string format)
    {
        DataRow newRow = dt.NewRow();

        newRow["Specifier"] = specifier;
        newRow["FormatName"] = name;
        newRow["Format"] = format;
        newRow["Culture"] = testCulture.ToString();
        newRow["Int"] = this.GetFormattedNumericString(testInt, format, testCulture);
        newRow["Double"] = this.GetFormattedNumericString(testDouble, format, testCulture);

        dt.Rows.Add(newRow);
    }

    private string GetFormattedNumericString(int testInt, string format, CultureInfo testCulture)
    {
        System.Threading.Thread.CurrentThread.CurrentCulture =
            System.Globalization.CultureInfo.CreateSpecificCulture("en-US");
        System.Threading.Thread.CurrentThread.CurrentUICulture =
            new System.Globalization.CultureInfo("en-US");

        try
        {
            return testInt.ToString(format, testCulture);
        }
        catch (Exception ex)
        {
            return ex.Message;
        }
    }

    private string GetFormattedNumericString(double testDouble, string format,
        CultureInfo testCulture)
    {
        System.Threading.Thread.CurrentThread.CurrentCulture =
            System.Globalization.CultureInfo.CreateSpecificCulture("en-US");
        System.Threading.Thread.CurrentThread.CurrentUICulture =
            new System.Globalization.CultureInfo("en-US");

        try
        {
            return testDouble.ToString(format, testCulture);
        }
        catch (Exception ex)
        {
            return ex.Message;
        }
    }
}

References:
Standard numeric format strings
Standard Numeric Format Strings Output Examples

Advertisements
 
Leave a comment

Posted by on May 16, 2012 in .NET, C#

 

Tags: , , ,

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

 
%d bloggers like this: