Suresh Chaudhary
Suresh Chaudhary

Reputation: 1639

How to convert negative values into $ format in C#.NET

When I use positive Double value like 10, 15, 20 and convert to $ using this formula:

String Val=Convert.ToDouble("10").ToString("c")

Then it returns current value, but if my value is -10 it returns ($10). It automatically appends () and add value between this for negative double value.

Can anyone tell me how to handle this?

Upvotes: 1

Views: 1337

Answers (3)

Iain Ballard
Iain Ballard

Reputation: 4818

That's the standard accounting way of sowing a negative money quantity. You should be able to update this for yourself by changing your Windows settings for currency.

If you want to have a explicitly specific format, you could try

var val = 10.0d; var str = val.ToString("$#0.##")

or similar.

John Sheehan has a set of cheat-sheets which are handy quick references for this at http://john-sheehan.com/blog/

Upvotes: 0

Nick Berardi
Nick Berardi

Reputation: 54854

You can change it by modifying this

http://msdn.microsoft.com/en-us/library/system.globalization.numberformatinfo.currencynegativepattern.aspx

System.Treading.Thread.CurrentThread.Culture.NumberFormatInfo.CurrencyNegativePattern = "(${0})";

Upvotes: 0

Davide Piras
Davide Piras

Reputation: 44605

This is a system regional specific setting, in general it's not a good idea to override it in your code because the user has decided to see negatives with () instead of with - in the control panel.

If you really want, you can use the NumberFormatInfo parameter in the overload of ToString but don't do it :)

Upvotes: 1

Related Questions