c# - 如何在 ASP NET MVC 中使用负数而不是括号显示货币?

标签 c# asp.net-mvc cultureinfo negative-number

我希望我的值(value)观显示在 View 中

喜欢:-$150.00

而不是:($150.00)

--

我想这就是我必须做的:

How do I display a negative currency in red?

但我不知道他所说的“BaseController类”是什么意思

最佳答案

这一切都归结为NumberFormatInfo.CurrencyNegativePattern .当听起来您想要 1 时,您可能得到了值 0。

不清楚您当前使用的是用户的 CultureInfo、服务器的还是其他的。但您始终可以克隆您正在使用的任何文化,然后修改 NumberFormatInfo

示例代码:

using System;
using System.Globalization;

class Test
{
    static void Main()
    {
        var original = new CultureInfo("en-us");
        // Prints ($5.50)
        Console.WriteLine(string.Format(original, "{0:C}", -5.50m));

        var modified = (CultureInfo) original.Clone();
        modified.NumberFormat.CurrencyNegativePattern = 1;        
        // Prints -$5.50
        Console.WriteLine(string.Format(modified, "{0:C}", -5.50m));
    }
}

关于c# - 如何在 ASP NET MVC 中使用负数而不是括号显示货币?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19058873/

相关文章:

c# - JSON 反序列化为具有私有(private) setter 的对象

ASP.NET MVC - 我需要呈现部分的操作方法。我该怎么做?

wpf - 多线程WPF应用程序设置线程CultureInfo

c# - 内联表达式是在运行时还是构建时编译的?

c# - 查找具有相似和不同字段的记录

c# - 比较两个数组或数组列表,找出相似和不同的值

javascript - 在 Firefox 上使用 Jquery+ASP.Net MVC 获取解析错误

asp.net-mvc - MVC4中的ActionLink没有解决 Controller 中的Action

c# - 以美国文化格式显示货币值

c# - 组合框中的 WPF CultureInfo,DisplayName 作为 DisplayMemberPath 在更改 SelectedUICulture 时不起作用