CSharp - Format NumberFormatInfo Type


The following code instantiates a NumberFormatInfo and change the group separator from a comma to a space.

We then use it to format a number to three decimal places:


using System;
using System.Globalization;

class MainClass/*  w w w  .j  a  v a2  s  . c  om*/
    public static void Main(string[] args)
        NumberFormatInfo f = new NumberFormatInfo();
        f.NumberGroupSeparator = " ";
        Console.WriteLine(12345.6789.ToString("N3", f));   // 12 345.679


The initial settings for a NumberFormatInfo are based on the invariant culture.

You can Clone an existing format provider:

NumberFormatInfo f = (NumberFormatInfo)

A cloned format provider is writable-even if the original was read-only.