25

I have a POCO with a decimal property called SizeUS. I would like to use data annotations to format the display of the decimal in a view. My SizeUS property is only displaying 2 decimal places in my view and I want it to display 4 decimal places. What is the proper data annotation to accomplish this ?

[DisplayFormat( ? )]
public decimal SizeUS {get; set;}
Bill Greer
  • 3,046
  • 9
  • 49
  • 80

1 Answers1

49
[DisplayFormat(DataFormatString="{0:#.####}")]

See Custom Format Strings for formats and DisplayFormatAttribute for examples

Johnbot
  • 2,169
  • 1
  • 24
  • 24
  • 4
    If you want the 4 decimals to display in edit mode also, use `ApplyFormatInEditMode = true` in the above `DisplyaFormat` declaration. – Shiva Dec 30 '13 at 23:06
  • What's the `{0:` bit mean/do? – ᴍᴀᴛᴛ ʙᴀᴋᴇʀ Oct 25 '16 at 11:12
  • 1
    @MattBaker it's a format item. `0` being the first item. See the documentation for [`string.Format`](https://msdn.microsoft.com/en-us/library/system.string.format(v=vs.110).aspx#Starting). – Johnbot Oct 25 '16 at 12:07
  • 2
    Remember that DisplayFormat is ignored in "TextBoxFor", check [this answer](https://stackoverflow.com/a/21135140/1216184) to format through TextBoxFor passing the template as argument – Ferran Salguero Oct 30 '18 at 11:51