Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
201 views
in Technique[技术] by (71.8m points)

c# - How to use NumberFormatInfo to remove parenthesis for negative values

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You can make a clone of the NumberFormat property in the CurrentCulture (this will ensure that you keep the correct currency symbol if you globalize your application). After that, just set the CurrencyNegativePattern property to the desired value and pass in your NumberFormatInfo in the double.ToString() function, like so:

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...