开发者

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

开发者 https://www.devze.com 2023-02-14 06:06 出处:网络
When I use positive Double value like 10, 15, 20 and convert to $ using this formula: String Val=Convert.ToDouble(\"10\").ToString(\"c\")

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 i开发者_运维问答t returns ($10). It automatically appends () and add value between this for negative double value.

Can anyone tell me how to handle this?


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 :)


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/


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})";
0

精彩评论

暂无评论...
验证码 换一张
取 消