如何使用数字格式信息删除负值的括号

本文关键字:信息删除 何使用 数字 格式 | 更新日期: 2023-09-27 18:37:05

我们目前正在使用以下命令在我们的 Web 应用程序中创建美元值:

string.Format("{0:C0}", dollarValue );

在此示例中,如果 dollarValue 为 145,则我们将看到:$145。如果是 -145,那么我们将看到 ($145) 而不是 -$145。请注意,对我们来说,dollarValue 是双精度值,但它可以是任何数字类型。我认为。

无论如何,我想要的是 145 美元

或 -145 美元。

现在,根据我的研究,可以使用NumberFormatInfo类来做到这一点。我不知道如何使用它,也找不到任何有效的例子。我确实在这里看到了这个问题:C# 创建一个自定义 NumberFormatInfo 以在货币值为 $0.00 时显示"免费",但从此问题链接到的 MSDN 示例似乎与我真正想要做的事情略有不同。

我意识到,我需要执行以下操作:

double dollarValue = -145d;
string myMoney = dollarValue.ToString( "C0", someIFormatProvider );

其中某些IFormatProvider可能是NumberFormatInfo类型。现在,我所做的是这样的:

NumberFormatInfo ni = new NumberformatInfo();
ni.CurrencyNegativePattern = 1; // The value 1 should mean not to use parenthesis
string myMoney = dollarValue.ToString( "C0", ni );

。并且我收到"实例是只读的"异常。虽然 CurrencyNegativePattern 属性的"文档"说您可以设置和获取值,但显然您不能。此外,NumberFormatInfo 是一个密封类。我无法轻松地基于它创建一个新类并覆盖该值。

我不知道如何处理这个问题。现在,我的解决方法是否定我的负值,并得到一个积极的结果,我再次做string.Format(...)。是的,我意识到前面没有负号,但是,当然,根据需要在结果前面添加"-"可以轻松解决。

有人能够为我提供一个在这种情况下如何正确有效地使用 NumbefFormatInfo 类的工作示例吗?谢谢。

如何使用数字格式信息删除负值的括号

可以在CurrentCulture中克隆NumberFormat属性(这将确保在全球化应用程序时保留正确的货币符号)。 之后,只需将 CurrencyNegativePattern 属性设置为所需的值,并在 double.ToString() 函数中传入您的NumberFormatInfo,如下所示:

NumberFormatInfo noParens = (NumberFormatInfo)CultureInfo.CurrentCulture.NumberFormat.Clone();
noParens.CurrencyNegativePattern = 1;
double dollarValue = -145d;
string output = dollarValue.ToString("C0", noParens); // outputs -$145

试试这个...

string.Format( "{0:$#.;-$#.}", -145 )

dollarValue.ToString("$#.;-$#.")
double dollarValue = -145d;
 System.Globalization.CultureInfo modCulture = new System.Globalization.CultureInfo("en-US");
 string mymoney = dollarValue.ToString("C", modCulture);
          MessageBox.Show(mymoney);

如果您的操作系统当前区域性是 en-US,则不需要以下行

System.Globalization.CultureInfo modCulture = new System.Globalization.CultureInfo("en-US");

代码将是

double dollarValue = -145d;
string mymoney = dollarValue.ToString("C");

和如果你想做数字格式信息

double dollarValue = -145d; 
System.Globalization.CultureInfo modCulture = new System.Globalization.CultureInfo("en-US");
     NumberFormatInfo number = modCulture.NumberFormat;
     string mymoney = dollarValue.ToString("C", number);

另一种方式

double dollarValue = -145d;
System.Globalization.CultureInfo modCulture = new System.Globalization.CultureInfo("en-US");
NumberFormatInfo number = modCulture.NumberFormat;
string mymoney = dollarValue.ToString(String.Format("C",number));
Console.WriteLine(mymoney);

"实例是只读的"表示您有一个只读的NumberFormatInfo实例。 您可以通过克隆只读实例来获取可写实例:

NumberFormatInfo writeable = (NumberFormatInfo) ni.Clone();

您确定无法分配货币负模式吗?我尝试过使用以下代码:

double dollarValue = -145d;
System.Globalization.NumberFormatInfo ni = new System.Globalization.NumberFormatInfo();
ni.CurrencyNegativePattern = 1;
dropDownList.Items.Add(new ListItem(dollarValue.ToString("C0", ni)));

这是成功的。

为了确保你留在"美国语境"中,我会做这样的事情:

double val = -145d;
NumberFormatInfo nfi = new CultureInfo("en-US").NumberFormat;
nfi.CurrencyNegativePattern = 1;
val.ToString("C0", nfi).Dump();

Dump来自 LinqPad,因此显示-$145

收到的错误感觉就像是来自您NumberFormatInfo如下所示:

NumberFormatInfo nfi = Thread.CurrentThread.CurrentCulture.NumberFormat;

在这种情况下,我会用这样的东西替换:

NumberFormatInfo nfi = (NumberFormatInfo)Thread.CurrentThread.CurrentCulture.NumberFormat.Clone();