CSharp - Format NumberFormatInfo Type

Introduction

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:

Demo

using System;
using System.Globalization;

class MainClass/*from   w w w  .  j a  va  2 s.com*/
{
    public static void Main(string[] args)
    {
        NumberFormatInfo f = new NumberFormatInfo();
        f.NumberGroupSeparator = " ";
        Console.WriteLine(12345.6789.ToString("N3", f));   // 12 345.679
    }
}

Result

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

You can Clone an existing format provider:

NumberFormatInfo f = (NumberFormatInfo)
                     CultureInfo.CurrentCulture.NumberFormat.Clone();

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