【问题标题】:More efficient way to format currency values格式化货币值的更有效方法
【发布时间】:2019-06-24 03:03:23
【问题描述】:

我想在 HTML 页面中将 decimal 数字显示为美元货币。例如,将 1209.27 显示为 $1,209 27

我没有在模型中使用[DataType(DataType.Currency)],而是直接在视图中格式化每个数字:

@Html.Raw(Regex.Replace(Regex.Replace(String.Format("{0:C}", Model.Price), "(?<=\\.)([^.]*$)", "<sup>&nbsp;$1</sup>"), "\\.<sup>", "<sup>"))

有没有更有效的方法来实现这种格式,而不是从头再来?

【问题讨论】:

标签: c# asp.net razor model-view-controller


【解决方案1】:

您可以考虑编写 Razor DisplayTemplate 或 custom html helper

【讨论】:

    【解决方案2】:

    根据需要使用 CSS 设置样式。

    <style type="text/css">
    .price { font-weight: bold;}
    .currency {}
    .decimals { font-size: 0.6em; font-weight: normal; vertical-align: baseline; top: -0.4em; position: relative; }
    </style>
    This item costs 
    <span class="price">
    <span class="currency">$</span>5,310<span class="decimals">.79</span></span> Dollars.
    

    在这里工作的小提琴: https://jsfiddle.net/a3vksgcu/1/

    这里的原始解决方案: https://css-tricks.com/forums/topic/solved-a-more-interesting-price-display-using-css/

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-01-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-04-25
      • 2012-05-31
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多