c# - C# 中负货币数字的全局变化格式

标签 c# asp.net c#-4.0

我们有一个大型 ASP.NET MVC 项目,其中所有输出到屏幕的数字都被格式化为货币(即 ToString("c")。但是,负数显示为 ()。例如:

decimal d = -8.88m;
Console.WriteLine(d.ToString("c"));
//outputs $(8.88)

这对我们的用户来说有点烦人,尤其是在文本框中。我们有几千个地方可以像这样将货币字段发送到屏幕,所以我们希望有一种方法可以全局更改格式。有吗?我见过的所有方法都表明您必须创建一个新的格式化程序,类似于:

 string curCulture = System.Threading.Thread.CurrentThread.CurrentCulture.ToString();
 System.Globalization.NumberFormatInfo currencyFormat =
     new System.Globalization.CultureInfo(curCulture).NumberFormat;
 currencyFormat.CurrencyNegativePattern = 1;

我们不希望更改我们所有的 ToString("c") 方法……有更好的方法吗?我的第一个想法是将我们的区域设置更改为澳大利亚,但意识到日期格式会搞砸。

最佳答案

Aliostad 很接近......在你的基础 Controller 中试试这个:

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

关于c# - C# 中负货币数字的全局变化格式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3994601/

相关文章:

c# - 最后从字符串中删除标点符号

ASP.NET Active Directory 成员资格提供程序和 SQL 配置文件提供程序

c# - 实现迭代器时,返回 IEnumerator 或 IEnumerable 有什么区别?

asp.net - 如何在asp.net中以编程方式编辑gridview?

c# - 如何向编译器保证泛型类型将具有某种属性?

c# - 使用 NotifyIcon 创建的 Windows 通知显示 "microsoft.explorer.notification"和 GUID

c# - 将 UserControl 转换为自定义控件

javascript - ASP.NET 代码隐藏文件在哪里执行? (客户端或服务器)

c# - 检查文本框是否包含任何数字

c# - 已完成的 c# 任务再次运行...为什么?