红色如何显示负货币? [英] How do I display a negative currency in red?

查看:116
本文介绍了红色如何显示负货币?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下的HTML code ...

I have the following html code...

<%=String.Format("{0:C0}", item.currency)%>

我所需要的货币格式,但显示这样我负...

I need the currency format but my negative is being displayed like this...

($2,345)

我想是红色的格式。我可以设置一个开关变量,但有一个更简单的方法?

I would like the format to be in red. I can set a toggle variable, but is there an easier way?

推荐答案

在我的项目,我想做同样的事情,而且也显示了负值。 - $ 2345号,而不是在括号

In my project, I wanted to do the same thing, but also display the negative value as "-$2345" rather than in parentheses.

要采取格式的照顾,我第一次增加了以下我BaseController类(其中,顾名思义,是我所有的控制器基类):

To take care of the format, I first added the following to my BaseController class (which, as the name implies, is the base class for all of my Controllers):

protected override void Initialize(System.Web.Routing.RequestContext requestContext)
{
    base.Initialize(requestContext);

    System.Globalization.CultureInfo modCulture = new System.Globalization.CultureInfo("en-US");
    modCulture.NumberFormat.CurrencyNegativePattern = 1;
    Thread.CurrentThread.CurrentCulture = modCulture;
}

请参阅<一href="http://msdn.microsoft.com/en-us/library/system.globalization.numberformatinfo.currencynegativepattern.aspx" rel="nofollow">http://msdn.microsoft.com/en-us/library/system.globalization.numberformatinfo.currencynegativepattern.aspx.这花了多少的格式照顾。至于红色的,我添加一个新的名为CSS类负:

See http://msdn.microsoft.com/en-us/library/system.globalization.numberformatinfo.currencynegativepattern.aspx. That took care of the formatting of the number. As for the red, I added a new css class called "negative":

.negative
{
    color: Red;
}

然后在我的.aspx文件:

And then in my .aspx file:

<% if (item.currency < 0.0M)
{ %>
<span class="negative"><%=String.Format("{0:C}", item.currency)%></span>
<% }
else
{ %>
<span><%=String.Format("{0:C}", item.currency)%></span>
<% } %>

把此在CSS类的好处是,对于动态网站,如果以后变正(或反之亦然,如果正值变为负),我可以使用jQuery简单地添加或从去除类跨度/每格的问题,使文字默认或红色。

The benefit of putting this in a css class is that, for dynamic sites, if it later becomes positive (or vice versa, if a positive value becomes negative), I can use jQuery to simply add or remove the class from the span/div in question and make the text default or red.

这篇关于红色如何显示负货币?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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