Use DisplayFormat to show 3 decimal places

user2816567 picture user2816567 · Jan 29, 2015 · Viewed 8.8k times · Source

I am a newbie to .NET. I cannot figure out the correct syntax to show three decimal places in my view.

In my model I currently have:

&lt;<Display(name:="Straight Rate")>
Public Property StraighRate() As Decimal
    Get
        Return mStraightRate
    End Get
    Set(ByVal value As Decimal)
        mStraightRate = value
    End Set
End Property

I know I need to use the DisplayFormat in my model, but I cannot figure out the syntax that will make it work.

Do I need to do anything additional in my View after I add the syntax for the DisplayFormat in my model?

Here is what I have in my current view:

@Html.DisplayFor(Function(modelItem) currentItem.StraightRate)

Answer

user3559349 picture user3559349 · Jan 29, 2015

Use the DisplayFormatAttribute. The DataFormatString property determines how the value is displayed in a DisplayTemplate

<Display(name:="Straight Rate")>
<DisplayFormat(DataFormatString:="{0:0.000}")>
Public Property StraighRate() As Decimal