1

I feel a bit dumb asking this... I need to format a number to show thousands separator, decimal mark, and exactly the precision that is needed to show all significan digits. I have a working solution, but feel there must be a better way:

double myNumber = 1234.56789;
myNumber.ToString("#,##0.#########################################################################################################################"); // see my problem with this?
// will yield "1,234.56789"

I googled both internets from start to end and couldn't find an answer to my problem. Answers on SO like remove trailing zeros and C# String.format a number with dynamic number of significant digits don't respect the thousands separator requirement. And both msdn articles for Standard Numeric Format Strings and Custom Numeric Format Strings have been read and re-read by me without bringing a breakthrough.

My attention was brought to c# how to String.Format decimal with unlimited decimal places? but unfortunately no answer came up with a more concise format string.

What format string would a sane and proficient developer use?

Community
  • 1
  • 1
Martin
  • 5,714
  • 2
  • 21
  • 41
  • does this help: http://samples.pdmlab.com/stringformatting? – Jens Mar 03 '15 at 14:20
  • @JensHorstmann - That may be more useful in English (being as most visitors here, and this website, is/are English speaking) – Jamiec Mar 03 '15 at 14:24
  • @Jamiec agreed. haven't recognized that it is german :| – Jens Mar 03 '15 at 14:25
  • @JensHorstmann: Actually I speak German. I viewed the page. But the tables there do not provide more or deeper insight than those that are shown in the two msdn articles I linked. – Martin Mar 03 '15 at 14:30
  • "both internets" all this time and there is one I don't know about?! :) – Fred Mar 03 '15 at 14:30
  • possible duplicate of [c# how to String.Format decimal with unlimited decimal places?](http://stackoverflow.com/questions/7795161/c-sharp-how-to-string-format-decimal-with-unlimited-decimal-places) – Richard Mar 03 '15 at 14:36
  • 1
    @Richard: Thanks for the link, checked it. I'm explicitly asking for a solution that works without the need for #,#.############################# – Martin Mar 03 '15 at 14:46
  • Remember that a `double` can only have 15-16 digits of precision so you don't need 50 hashes - `#,#.################` should suffice. Another way would be: `"#,#."+new string('#',16)` – D Stanley Mar 03 '15 at 15:33
  • @Martin - the answer to the duplicate question concluded that No, this was not possible, and that you're doing it the right way as long as you have sufficient ###s to cover the data type you're using, so it is covering your question. – Richard Mar 03 '15 at 17:58

2 Answers2

1

Honestly, I dont see a great problem with your formatting string, so long as you dont repeat it everywhere (put it in a class full of constants, and reuse).

The nearest standard format string is probably along the lines of N50, but this will display zeros up to the length specified, so your example displays as

1,234.56789000000000000000000000000000000000000000000000
Jamiec
  • 133,658
  • 13
  • 134
  • 193
  • agree with this solution. You could use myNumber.ToString("N20"); for double or N30 for decimal and after that the percision of double / decimal is a bigger problem than the appearance of the string values. By me in debug both solution show only 11 Digits after the Point, but the N20 Solution shows zeros after the 11th Digit... – Jens Mar 03 '15 at 14:45
  • @JensHorstmann That's because a double can only hold 15-16 decimal digits of precision. With 4 digits to the left of the decimal that only leaves 11 to the right. – D Stanley Mar 03 '15 at 15:42
-1

The reason you haven't found the answer you want from Googling is because there's a problem with your question.

Doubles are not stored in decimal form, they're binary numbers. So the concept of a decimal-based "significant digit" doesn't really make sense. As an example, the number 0.1 cannot be represented precisely in binary (much like 1/3 can't be represented precisely in decimal notation).

You could so something like @Jamiec suggested - and it's probably about what you want (although N50 is way overkill, you only get about 17 max significant digits converting a binary to a double). But, you should know that's not really writing out the significant digits. Fundamentally, you can't write out all the significant digits the computer has calculated a number to in decimal, because it calculated it in binary. The computer will have to convert it to decimal first before it writes it out, and that will necessarily cause a loss of precision.

Pharylon
  • 9,796
  • 3
  • 35
  • 59