-1

In MSDN documentation they say the "D" (or "d") format specifier is both for decimal numbers and dates but in fact it only formats a date (see this question also):

string.Format("{0:d}", DateTime.Now) // Works
string.Format("{0:d}", 12.998m) // Exception

So, why is that and when is it possible to use it to format a decimal?

Community
  • 1
  • 1
bcl
  • 147
  • 4
  • 15
  • Where in the documentation does it say that the `D` specifier is also for decimal numbers? – Lasse V. Karlsen Feb 14 '17 at 11:00
  • @JayGould - *format strings* are case sensitive. You would get exactly the same situation making these same calls from VB.Net, which is generally consider a case insensitive language. – Damien_The_Unbeliever Feb 14 '17 at 11:00
  • @LasseV.Karlsen, look at first table [here](https://msdn.microsoft.com/en-us/library/dwhawy9k(v=vs.110).aspx#DFormatString), second row, second column, it clearly say `Decimal` ;) – Sinatr Feb 14 '17 at 11:02
  • @Sinatr Now look at second row *third* column :) (I agree, the *name* is misleading) – Jamiec Feb 14 '17 at 11:07
  • Look at the column header for the second column and you see "Name". `Decimal` is the **name** of this format specifier, not the type it applies to. – AakashM Feb 14 '17 at 11:07

1 Answers1

5

The docs you linked to are pretty clear on this point - D is only supported by integral types

The "D" (or decimal) format specifier converts a number to a string of decimal digits (0-9), prefixed by a minus sign if the number is negative. This format is supported only for integral types.

Source: https://msdn.microsoft.com/en-us/library/dwhawy9k(v=vs.110).aspx#DFormatString

Jamiec
  • 133,658
  • 13
  • 134
  • 193
  • Thank you Jamiec, I didn't notice, still a bit confusing though, in my opinion. They could have used another letter. :) – bcl Feb 14 '17 at 11:19
  • 1
    @drevis yes, having a format called "decimal" only be useful for integral types is a bit.....confusing. – Jamiec Feb 14 '17 at 11:29