3

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)
user2816567
  • 43
  • 1
  • 1
  • 5
  • you should always have a String method which returns it in the correct format and use that to bind to the UI (unless of course you are editing/input) Have the String method do the formatting and return the value with formatting back. – Ahmed ilyas Jan 29 '15 at 19:09
  • https://msdn.microsoft.com/en-us/library/0c899ak8(v=vs.110).aspx, As I understand you need the format `"0.000"`. – Mark Shevchenko Jan 29 '15 at 19:15

2 Answers2

7

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
0

mStraightRate = Format(value, ##.##.##) you can use the Pound signs how you wish for currency you can simply put.. mStraightRate = Format(value,"currency") there are more options but for custom use the top.

Wiz12
  • 1
  • 1
  • sorry noticed you wanted 3 decimal place here is that code. `mStraightRate = Format(value, ##.###)` – Wiz12 Jan 29 '15 at 20:21
  • I tried upating my model to include the above:
    Public Property StraighRate() As Decimal Get Return mStraightRate End Get Set(ByVal value As Decimal) mStraightRate = Format(value, ##.###) End Set End Property
    I am getting a build error saying expression expected....
    – user2816567 Jan 29 '15 at 20:38
  • hmm try mStraightRate.Value = – Wiz12 Jan 29 '15 at 21:05