3

How do I get a number formatted, such that the (culture-specific) thousands separator is also used after the decimal point?

Check this code (NUnit test case, passes as-is):

    [Test]
    [SetCulture("en-US")]
    public void FormattingOnly()
    {
        // a large number
        double result = 1E9 + 0.212324;
        string printout = result.ToString("N9");
        // Would like to have this as "1,000,000,000.212,320,000"
        Assert.AreEqual("1,000,000,000.212320000", printout); 

        result = 0.212324;
        printout = result.ToString("N9");
        // Hard to read, should better be "0.212,324,000"
        Assert.AreEqual("0.212324000", printout);

        result = 0.212324;
        printout = result.ToString("E9");
        // Even harder to read, needs users to have a PhD ;)
        Assert.AreEqual("2.123240000E-001", printout);

        result = 1E9 + 0.212324;
        printout = result.ToString("E");
        // Completely worthless for many users
        Assert.AreEqual("1.000000E+009", printout);
    }

I'm trying to find the most readable form, that even users without a math background can understand. In my application, users have the possibility to set the number of digits in output formatting according to their needs.

Note: this is not a duplicate of .NET String.Format() to add commas in thousands place for a number. There it's about thousands separators in general.

PMF
  • 14,535
  • 3
  • 23
  • 49
  • 5
    Thousands separators should NOT be used in decimal places. Why do you need to display numbers with so many decimal places? I'd say the problem here is showing too much precision. (Note that it simply doesn't work properly if you try to display thousand separators for decimal places.) – Matthew Watson Oct 02 '18 at 10:54
  • What *do* you want to display? A number that mixes up decimal and thousand separators is definitely *not* readable. Formatting with a set number of digits is already possible when you use a format string either by passing the number of digits or using digit placeholders. – Panagiotis Kanavos Oct 02 '18 at 10:55
  • And no, `1,000,000,000.212,320,000` isn't readable or desirable. Anyone seeing this would assume its 1E18. Someone would have to read all of this thing carefully to determine if there are any decimal points in there. – Panagiotis Kanavos Oct 02 '18 at 10:58
  • 2
    Here's another example of why it's a bad idea to use thousands separators in decimal places. Consider two identical numbers shown to different numbers of decimal places, `0.1111` and `0.11110`. If you use thousand separators they would be displayed as `0.1,111` and `0.11,110` - two identical numbers but with different positions for the thousands separators isn't an improvement. – Matthew Watson Oct 02 '18 at 11:00
  • A *very* common way of grouping digits is to use spaces in input and display masks instead of thousand separators. That's how spreadsheets and masked edit boxes work for example. Check Wikipedia's [Data versus mask](https://en.wikipedia.org/wiki/Decimal_separator#Data_versus_mask) section on digital separators. Both `149 597 870 700 ` and `3.14159 26535 89793 238 46` are clear and easily readable – Panagiotis Kanavos Oct 02 '18 at 11:11
  • I really can't understand where these weird comments come from. It it __not your business__ why somebody wants these decimal places!! And __obviously__ some sort of separator would help readability! All your arguments against are totally contrived. And: __Just as obvisouly__ they ought to be counted from the decimal point, not from the end, good heavens! – TaW Oct 02 '18 at 11:13
  • @TaW on the contrary, I suspect you haven't considered them. Yes, errors due to bad representations have happened, a lot. That's why doctors *never* use `V` and `X` to check lists. It's *too* easy to make a mistake. And yes, separators are used for readability. There's a very common way to do so using spaces – Panagiotis Kanavos Oct 02 '18 at 11:16
  • The same kind of nonsense was offered [here](https://stackoverflow.com/questions/26202493/decimal-group-seperator-for-the-fractional-part). Pathetic. If you can't tell points from commas then a decimal separator before the decimal points will also lead to terrible confusion! – TaW Oct 02 '18 at 11:18
  • @TaW if you disagree with the Wikipedia article feel free to edit it – Panagiotis Kanavos Oct 02 '18 at 11:19
  • Why would I? It recommends spaces both before and after the decimal points, which is fine by me. – TaW Oct 02 '18 at 11:21
  • @TaW Spaces are fine after the decimal mark; commas are not. – Matthew Watson Oct 02 '18 at 11:21
  • @Matthew. And that is your opinion, based on what? Never mind.. – TaW Oct 02 '18 at 11:22
  • @Pan: Grouping pi decimals in groups of five is both surprising (always bad) and clearly not readable at all. – TaW Oct 02 '18 at 11:24
  • 2
    @TaW I shall adhere to the guidelines issued by the [Bureau of International Weights and Measures](https://www.bipm.org/en/publications/si-brochure/section5-3-4.html) `for numbers with many digits the digits may be divided into groups of three by a thin space, in order to facilitate reading. Neither dots nor commas are inserted in the spaces between groups of three.` (although of course a normal space has to be used unless typesetting). – Matthew Watson Oct 02 '18 at 11:33
  • Well, it is good news that you are at least grouping it right, as opposed to your 2nd comment :-) - I personally use spaces as well (also not supported) btw, but the question is valid and I would certainly prefer to get more versatlie support from the system, SI or not.. I also live in Europe and our decimal point is comma (ach, COBOL memories poppin' up ;-)), separator is point, so imo it looks a lot clearer than the other way round, anyway.. - Other than that: Good find. So do we agree that it would be nice to get support to have spaces as fractional decimal separators? – TaW Oct 02 '18 at 12:40
  • 3
    @TaW I totally agree - there should be first-class support for using spaces for grouping both before AND after the decimal mark. It should be supported by a standard numeric format string. – Matthew Watson Oct 02 '18 at 14:10
  • @MatthewWatson I'm totally with you and TaW here. The question is not about _which_ kind of separator to use, but how to insert it. My locale generally uses ' as thousands separator, which is unambiguous. That the english standard using comma is confusing (especially since there are locales which use the comma as decimal separator) is completely another story. – PMF Oct 03 '18 at 06:04

0 Answers0