Use a custom thousand separator in C#
Asked Answered
H

5

43

I'm trying not to use the ',' char as a thousand separator when displaying a string, but to use a space instead. I guess I need to define a custom culture, but I don't seem to get it right. Any pointers?

eg: display 1000000 as 1 000 000 instead of 1,000,000

(no, String.Replace() is not the solution I'd like to use :P)

Henceforth answered 15/4, 2009 at 15:8 Comment(5)
What's wrong with using String.Replace()?Fortuna
@Jon B - because Replace wouldn't be culture independent. What if your running on a computer where the thousand sep is . ?Gelderland
and since I'm already formatting the number, it'd be cluttering the codeHenceforth
@Peter - I belive you could use InvariantCulture and then do String.Replace(). I'm not arguing this as a good solution, I was just curious why the OP was opposed to it.Fortuna
Because it is a hack?Observance
L
80

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
    }
}
Lais answered 15/4, 2009 at 15:12 Comment(0)
K
9

Create your own NumberFormatInfo (derivative) with a different thousand separator.

Kukri answered 15/4, 2009 at 15:12 Comment(0)
P
7

There's a slightly simpler version of Jon Skeet one :

using System;
using System.Globalization;

class Test
{
    static void Main()
    {
        NumberFormatInfo nfi = new NumberFormatInfo {NumberGroupSeparator = " ", NumberDecimalDigits = 0};

        Console.WriteLine(12345678.ToString("n", nfi)); // 12 345 678
    }
}

And the 'nfi' initialization could be skipped and put directly as parameter into the ToString() method.

Personification answered 24/1, 2014 at 13:14 Comment(1)
That's basically John Skeet's answer rewritten. Not exactly creative or shorter!Pentangular
P
3

Easiest way...

num.ToString("### ### ### ### ##0.00")
Pontine answered 15/4, 2009 at 15:18 Comment(1)
Although this works it will actually produce spaces in front of the number. For example the number 1 would have 4 spaces in front of it. So at least you will have to trim it. I think the best solution is to adjust the NumberFormatInfo if you want to override the Culture default format.Ibrahim
A
0

I wanted to

  • have space as a thousand separator,
  • have up to two decimals and
  • optionally be negative.

This code solves it:

var regex = new Regex(@"(^|(?<=-))\s*");
var format = "### ### ### ### ##0.##";
decimal number = 123456789.54m;
var str = regex.Replace(number.ToString(format), ""); // str = "123 456 789.54"

Here are examples of how this behaves for various numbers:

regex.Replace(0m.ToString(format), "")
"0"
regex.Replace(123m.ToString(format), "")
"123"
regex.Replace(123456789m.ToString(format), "")
"123 456 789"
regex.Replace(123456789.1m.ToString(format), "")
"123 456 789.1"
regex.Replace(123456789.12m.ToString(format), "")
"123 456 789.12"
regex.Replace(123456789.1234m.ToString(format), "")
"123 456 789.12"
regex.Replace((-123456789.1234m).ToString(format), "")
"-123 456 789.12"
regex.Replace((-1234m).ToString(format), "")
"-1 234"
regex.Replace((-1m).ToString(format), "")
"-1"
Aviva answered 24/7, 2023 at 13:42 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.