【问题标题】:Use DisplayFormat to show 3 decimal places使用 DisplayFormat 显示 3 位小数
【发布时间】:2015-03-29 03:01:17
【问题描述】:

我是 .NET 的新手。我无法弄清楚在我的视图中显示三位小数的正确语法。

在我目前的模型中:

&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

我知道我需要在我的模型中使用 DisplayFormat,但我无法弄清楚使它工作的语法。

在我的模型中添加 DisplayFormat 的语法后,我是否需要在视图中执行任何其他操作?

这是我目前的观点:

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

【问题讨论】:

  • 你应该总是有一个 String 方法,它以正确的格式返回它并使用它来绑定到 UI(当然,除非你正在编辑/输入)让 String 方法进行格式化并返回带有格式的值。
  • msdn.microsoft.com/en-us/library/0c899ak8(v=vs.110).aspx,据我了解,您需要"0.000" 的格式。

标签: asp.net-mvc vb.net displayformat


【解决方案1】:

使用DisplayFormatAttributeDataFormatString 属性确定值在DisplayTemplate 中的显示方式

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

【讨论】:

    【解决方案2】:

    mStraightRate = Format(value, ##.##.##) 你可以使用英镑符号来表示你想要的货币,你可以简单地放.. mStraightRate = Format(value,"currency") 还有更多选项,但用于自定义使用顶部。

    【讨论】:

    • 抱歉,您需要小数点后 3 位,这就是该代码。 mStraightRate = Format(value, ##.###)
    • 我尝试更新我的模型以包含上述内容:
      公共属性 StraighRate() As Decimal Get Return mStraightRate End Get Set(ByVal value As Decimal) mStraightRate = Format(value, ##. ###) End Set End Property
      我收到一个构建错误,提示表达式预期......
    • 嗯,试试 mStraightRate.Value =
    猜你喜欢
    • 1970-01-01
    • 2021-10-30
    • 1970-01-01
    • 2012-05-22
    • 1970-01-01
    • 2021-04-18
    • 1970-01-01
    • 1970-01-01
    • 2016-09-09
    相关资源
    最近更新 更多