.net 在括号中格式化负数但不使用 $ 符号?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8344575/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Format Negative numbers in parenthesis BUT NOT with $ symbol?
提问by user715993
I have seen all over the internet to format a NEGATIVE double value with a parenthesis WITH a $symbol ie. currency type.
我在互联网上看到过用带$符号的括号来格式化负双值,即。货币类型。
I am looking for a .NET format string, to format
我正在寻找一个 .NET 格式字符串来格式化
12345.67 = 12,345.67
-12345.67 = (12,345.67)
回答by Edmund Schweppe
MSDN on conditional formattingto the rescue!
MSDN 上的条件格式来拯救你!
You can specify up to three different sections of your format string at once, separating them with semicolons. If you specify two format string sections, the first is used for positive and zero values while the second is used for negative values; if you use three sections, the first is used for positive values, the second for negative values, and the third for zero values.
您一次最多可以指定格式字符串的三个不同部分,用分号分隔它们。如果指定两个格式字符串部分,第一个用于正值和零值,而第二个用于负值;如果使用三个部分,第一个用于正值,第二个用于负值,第三个用于零值。
The output from this C# code:
此 C# 代码的输出:
string fmt1 = "#,##0.00";
string fmt2 = "#,##0.00;(#,##0.00)";
double posAmount = 12345.67;
double negAmount = -12345.67;
Console.WriteLine("posAmount.ToString(fmt1) returns " + posAmount.ToString(fmt1));
Console.WriteLine("negAmount.ToString(fmt1) returns " + negAmount.ToString(fmt1));
Console.WriteLine("posAmount.ToString(fmt2) returns " + posAmount.ToString(fmt2));
Console.WriteLine("negAmount.ToString(fmt2) returns " + negAmount.ToString(fmt2));
is:
是:
posAmount.ToString(fmt1) returns 12,345.67
negAmount.ToString(fmt1) returns -12,345.67
posAmount.ToString(fmt2) returns 12,345.67
negAmount.ToString(fmt2) returns (12,345.67)

