我们目前正在使用下面为我们的web应用程序中创建的美元值:
的String.Format({0:C0},dollarValue);
在这个例子中,如果dollarValue是145,然后我们会看到:$ 145。如果是-145,然后我们会看到($ 145),而不是 - $ 145需要注意的是对我们来说,dollarValue是双重的,但它可以是任何数类型。我想。
不管怎样,我想是因为它是要么$ 145个或 - $ 145
现在,据我的研究,有可能使用的NumberFormatInfo类来做到这一点。我想不出如何使用它,我无法找到任何有效的例子。我没有看到这个问题在这里:C#创建自定义的NumberFormatInfo显示"免费"当一种货币的价值为$ 0.00包装但是从MSDN的例子联系到这个问题似乎从我真正想要做的有点不同。
我意识到,我需要做的事情如下:
双dollarValue = -145d;
字符串myMoney = dollarValue.ToString(C0,someIFormatProvider);
在这里someIFormatProvider可能类型的NumberFormatInfo的。现在,我所做的是这样的:
的NumberFormatInfo NI =新的NumberFormatInfo();
ni.CurrencyNegativePattern = 1; //值1应该是指不使用括号
字符串myMoney = dollarValue.ToString(C0,NI);
......,我得到一个实例是只读的异常。虽然文档为CurrencyNegativePattern物业说,你可以设置和获取价值,很显然,你不能。此外,的NumberFormatInfo是一个密封类。我不能轻易地创建基于关闭它一个新的类,并覆盖值。
我在一个不知如何处理这个问题。现在,我的解决方法是只否定我的负值,并有一个积极的结果,我的的String.Format(...)
再这样做。是的,我知道有一个在这方面没有任何负面的迹象,但是,当然,这是很容易加入解决了 - 的结果需要前面
会有人能为我提供了如何有效地正确使用NumbefFormatInfo类在这种情况下工作的例子吗?谢谢你。
解决方案 双dollarValue = -145d;
System.Globalization.CultureInfo modCulture =新System.Globalization.CultureInfo(EN-US);
字符串mymoney = dollarValue.ToString(C,modCulture);
的MessageBox.show(mymoney);
如果您的操作系统当前区域性为en-US,那么你不需要下面的行
System.Globalization.CultureInfo modCulture =新System.Globalization.CultureInfo(EN-US);
在code将
双dollarValue = -145d;
字符串mymoney = dollarValue.ToString(C);
如果你想要做的NumberFormatInfo
双dollarValue = -145d;
System.Globalization.CultureInfo modCulture =新System.Globalization.CultureInfo(EN-US);
的NumberFormatInfo数= modCulture.NumberFormat;
字符串mymoney = dollarValue.ToString(C号);
另一种方式
双dollarValue = -145d;
System.Globalization.CultureInfo modCulture =新System.Globalization.CultureInfo(EN-US);
的NumberFormatInfo数= modCulture.NumberFormat;
字符串mymoney = dollarValue.ToString(的String.Format(C,数));
Console.WriteLine(mymoney);
We're currently using the following for creating US dollar values in our web application:
string.Format("{0:C0}", dollarValue );
In this example, if dollarValue is 145, then we'll see: $145. If it is -145, then we'll see ($145) rather than -$145. Note that for us, dollarValue is a double, but it could be any number-type. I think.
Anyway, what I want is for it to be either $145 or -$145.
Now, according to my research, it might be possible to do this using the NumberFormatInfo class. I can't figure out how to use it, and I can't find any valid example. I did see this question here: C# creating a custom NumberFormatInfo to display "Free" when a currency value is $0.00 but the example from MSDN linked to from this question seems a bit different from what I really want to do.
I realize, I will need to do something of the following:
double dollarValue = -145d;
string myMoney = dollarValue.ToString( "C0", someIFormatProvider );
where someIFormatProvider is likely of type NumberFormatInfo. Now, what I've done is this:
NumberFormatInfo ni = new NumberformatInfo();
ni.CurrencyNegativePattern = 1; // The value 1 should mean not to use parenthesis
string myMoney = dollarValue.ToString( "C0", ni );
...and I get an "Instance is Read-Only" exception. While the "documentation" for the CurrencyNegativePattern property says you can SET and GET the value, apparently, you can't. Also, NumberFormatInfo is a sealed class. I can't easily create a new class based off it and override the value.
I'm at a loss as to how to deal with this. Right now, my workaround is to just negate my negative value and have a positive result that I do the string.Format(...)
again. Yes, I realize there is no negative sign in front of this, but, of course, that's easily resolved by adding a "-" in front of the result as needed.
Would someone be able to provide me with a working example of how to effectively use the NumbefFormatInfo class correctly in this situation? Thanks.
解决方案double dollarValue = -145d;
System.Globalization.CultureInfo modCulture = new System.Globalization.CultureInfo("en-US");
string mymoney = dollarValue.ToString("C", modCulture);
MessageBox.Show(mymoney);
If your Operating System current culture is en-US then you dont need the following line
System.Globalization.CultureInfo modCulture = new System.Globalization.CultureInfo("en-US");
The code will be
double dollarValue = -145d;
string mymoney = dollarValue.ToString("C");
and If you want to do with NumberFormatInfo
double dollarValue = -145d;
System.Globalization.CultureInfo modCulture = new System.Globalization.CultureInfo("en-US");
NumberFormatInfo number = modCulture.NumberFormat;
string mymoney = dollarValue.ToString("C", number);
another way
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);