Use a Custom Thousand Separator in C#

Use a custom thousand separator in C#

I suggest you find a NumberFormatInfo which most closely matches what you want (i.e. it's right apart from the thousands separator), call Clone() on it and then set the NumberGroupSeparator property. (If you're going to format the numbers using currency formats, you need to change CurrencyGroupSeparator instead/as well.) Use that as the format info for your calls to string.Format etc, and you should be fine. For example:

using System;
using System.Globalization;

class Test
{
static void Main()
{
NumberFormatInfo nfi = (NumberFormatInfo)
CultureInfo.InvariantCulture.NumberFormat.Clone();
nfi.NumberGroupSeparator = " ";

Console.WriteLine(12345.ToString("n", nfi)); // 12 345.00
}
}

How would I separate thousands with space in C#

Pass in a custom NumberFormatInfo with a custom NumberGroupSeparator property, and use the #,# format to tell it to do number groups. This example uses the invariant culture's number format as its basis.

var nfi = (NumberFormatInfo)CultureInfo.InvariantCulture.NumberFormat.Clone();
nfi.NumberGroupSeparator = " ";
string formatted = 1234897.11m.ToString("#,0.00", nfi); // "1 234 897.11"

Comma as decimal separator space as thousand separator C#

You have to specify both format string and format info:

  decimal source = 1234567890.987654m;

string result = source.ToString(
"###,###,###,###.###",
new NumberFormatInfo() {
NumberGroupSeparator = " ",
NumberDecimalSeparator = ","});

If it's too complex and you have to format many currencies in such a way, you can modify the culture:

  CultureInfo culture = CultureInfo.CurrentCulture.Clone() as CultureInfo;

culture.NumberFormat.CurrencyGroupSeparator = " ";
culture.NumberFormat.CurrencyDecimalSeparator = ",";
culture.NumberFormat.CurrencySymbol = "";
culture.NumberFormat.CurrencyDecimalDigits = 3;
culture.NumberFormat.CurrencyGroupSizes = new int[] {3};

CultureInfo.CurrentCulture = culture;

...

decimal source = 1234567890.987654m;

string result = source.ToString("c");

How to insert a thousand separator (comma) with convert to double

For complete custom control, use ... .ToString("#,##0.00") or variations thereof. The . and , will be replaced by culture dependent symbols. In most of europe you'd get 1.234,56.

Another useful picture is 0.0#.

To use a pattern depending on the users (or on a selected) culture, use The Numeric ("N") Format Specifier, as in .ToString("N") or "... {0:N}".

String.Format an integer to use a thousands separator with decimal value in danish culture

You can refer to Standard Numeric Format Strings and use

string.Format("{0:N2}", 1234.56)

You may also specify the culture manually, if danish is not your default culture:

var danishCulture = CultureInfo.CreateSpecificCulture("da-DK");
string.Format(danishCulture, "{0:N2}", 1234.56);

see MSDN Reference for CultureInfo

String.Format decimal with both thousand separators and forced decimal places

In a custom numeric format string a period (.) is used for a "localized decimal separator". Ie. even if your current locale uses a comma as a decimal separator you should use a period in a format string. Similarly comma (,) is used for the localised thousands separator.

As your format puts the comma after the period things are going to get confused (thousands separators don't apply after the decimal point).

So try:

 String.Format("{0:#,##0.000}", input);

(Using # for digits to only include if input is large enough.)

How to specify a custom thousands separator in a format strings

As far as I understand the Custom Numeric Format Strings, the , symbol is to place the group separator into the formatting (and not to specify which character to use). To make it an explicit symbol, you need to use the escape character \.

v.ToString(@"#\,###\,###")

Which has the disadvantage that the format string depends on the size of the number. In this case, you always get two commas in the resulting string, even if the number is smaller or higher.

To override the group separator, you need to specify your own NumberFormatInfo.

Got this test working:

decimal v = 2084000.7621m;

// clone the InvariantCulture to avoid specifying everything from scratch
var myNumberFormat = (NumberFormatInfo)CultureInfo.InvariantCulture.NumberFormat.Clone();
myNumberFormat.NumberGroupSeparator = "@";
myNumberFormat.NumberDecimalSeparator = "*";
Assert.AreEqual("2@084@000*7621", v.ToString("#,###,###.####", myNumberFormat));

It actually doesn't matter where and how many group separators you put into the format string. You could also make it: "#,#.####" with the exact same result. (Thats the advantage of using the group separator character, in contrast to the first solution).



Related Topics



Leave a reply



Submit