简体   繁体   中英

More efficient way to format currency values

I would like to display decimal numbers as USD currency within HTML pages. For example, display 1209.27 as $1,209 27 .

Instead of using [DataType(DataType.Currency)] in the models, I format each number directly in the view:

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

Is there a more efficient way to achieve this format instead of repeating this all over?

您可以考虑编写Razor DisplayTemplate或自定义html帮助器

Use CSS to style as you wish.

<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.

working fiddle here: https://jsfiddle.net/a3vksgcu/1/

Original solution here: https://css-tricks.com/forums/topic/solved-a-more-interesting-price-display-using-css/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM