字符串以货币双重价值无法正确显示格式。 [英] String Formatting with currency double values not displaying correctly.

查看:90
本文介绍了字符串以货币双重价值无法正确显示格式。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用MVC3与需要显示格式化的美元金额如$ 1,200.00详细视图。该控制器传递一个双精度值在这种情况下MonthlyMortgage到视图。 code的但是,下面的线,不显示正确的字符串格式。什么是显示为$ 1200.00,而我需要的是$ 1,200.00。

I'm using MVC3 with a detailed view that needs to display a formatted dollar amounts like $1,200.00. The controller is passing in a double value in this case for MonthlyMortgage to the view. The line of code below however, does not display the correct string formatting. What is displayed is $1200.00, and what I need is $1,200.00.

我曾尝试:

$@String.Format("{0:c}", Html.DisplayFor(model => model.MonthlyMortgage))

和我已经试过这样:

$@String.Format("{0:#,###,###,##0.000}", Html.DisplayFor(model => model.MonthlyMortgage))

有人请搭乘飞机这是为什么不工作?

Can someone please enplane why this is not working?

推荐答案

您不需要使用 Html.DisplayFor ,因为它会返回 MvcHtmlString 所以的String.Format 不适用。

You don't need to use Html.DisplayFor because it will return MvcHtmlString so the string.Format doesn't apply.

只需使用的String.Format 你的模型:

@String.Format("{0:c}", Model.MonthlyMortgage)

请注意,你并不需要$符号了,因为 {0:C} 将照顾它。

Note you don't need the '$' sign anymore because the {0:c} will take care of it.

这篇关于字符串以货币双重价值无法正确显示格式。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆