C# 自定义数字格式字符串以始终显示符号

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/348201/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-04 00:03:47  来源:igfitidea点击:

Custom numeric format string to always display the sign

c#.netformattingstring-formatting

提问by Craig Shearer

Is there any way I can specify a standard or custom numeric format string to always output the sign, be it +ve or -ve (although what it should do for zero, I'm not sure!)

有什么方法可以指定标准或自定义数字格式字符串来始终输出符号,无论是 +ve 还是 -ve(尽管它应该为零做什么,我不确定!)

采纳答案by gcores

Yes, you can. There is conditional formatting. See Conditional formatting in MSDN

是的你可以。有条件格式。请参阅MSDN 中的条件格式

eg:

例如:

string MyString = number.ToString("+0;-#");

Where each section separated by a semicolon represents positive and negative numbers

其中由分号分隔的每个部分表示正数和负数

or:

或者:

string MyString = number.ToString("+#;-#;0");

if you don't want the zero to have a plus sign.

如果您不希望零带有加号。

回答by Luk

Beware, when using conditional formatting the negative value doesn't automatically get a sign. You need to do

请注意,使用条件格式时,负值不会自动获得符号。你需要做

string MyString = number.ToString("+#;-#;0");

回答by Edward

You can also use format strings in string.Format(); the format string is separated from the index with a colon (':')

您还可以在 string.Format(); 中使用格式字符串。格式字符串用冒号 (':') 与索引分开

var f = string.Format("{0}, Force sign {0:+#;-#;+0}, No sign for zero {0:+#;-#;0}", number);

For number { +1, -1, 0 } this gives:

对于数字 { +1, -1, 0 } 这给出:

1, Force sign +1, No sign for zero +1
-1, Force sign -1, No sign for zero -1
0, Force sign +0, No sign for zero 0

1,力符号+1,零无符号+1
-1,力符号-1,零无符号-1
0,力符号+0,零无符号0

You can also use an interpolated string instead of string.Formatto obtain the same result:

您还可以使用内插字符串而不是string.Format获得相同的结果:

var f = $"{number}, Force sign {number:+#;-#;+0}, No sign for zero {number:+#;-#;0}";

回答by Kamil Szot

Contrary to the other answers it seems that if you want to get +1, -1, +0 (for arguments 1, -1, 0) you need to use the format:

与其他答案相反,似乎如果您想获得 +1、-1、+0(对于参数 1、-1、0),您需要使用以下格式:

String.Format("{0:+#;-#;+0}", 0));  // output: +0

or

或者

String.Format("{0:+0;-#}", 0));  // output: +0

If you use just +#;-#it will display just +(not +0) for 0.

如果您只使用+#;-#它,它将只显示+(not +0) 为 0。

The "#" Custom Specifier(at https://msdn.microsoft.com/en-us/library/0c899ak8.aspx)

“#”自定义说明符(位于https://msdn.microsoft.com/en-us/library/0c899ak8.aspx

Note that this specifier never displays a zero that is not a significant digit, even if zero is the only digit in the string. It will display zero only if it is a significant digit in the number that is being displayed.

请注意,此说明符从不显示不是有效数字的零,即使零是字符串中唯一的数字。只有当它是正在显示的数字中的有效数字时,它才会显示零。

Also please keep in mind that if you need any decimal precision you need to specify it like that:

另外请记住,如果您需要任何小数精度,您需要像这样指定它:

String.Format("{0:+0.##;-#.##}", 0));  // output: +0

or, if you wan't zeros to always show up, like that:

或者,如果你不想总是出现零,就像这样:

String.Format("{0:+0.00;-#.00}", 0));  // output: +0.00

回答by Tom Blodget

For a numeric expression of any type:

对于任何类型的数值表达式:

+###,###,###,###,###,###,###,###,###,##0.###,###,###,###,###,###,###,###,###,###;-###,###,###,###,###,###,###,###,###,##0.###,###,###,###,###,###,###,###,###,###;0

Use three parts for three cases: positive;negative;zero

三种情况使用三部分:正;负;零

Other aspects of the example:

示例的其他方面:

  • Zero is not signed. You could have it show as anything, such as "zero".

  • Absolute values less than one have a leading 0 before the decimal point. Adjust to taste.

  • Number of digits is for the largest and smallest absolute decimal values. Adjust to taste.

  • Decimal point character is culture-specific. .NET substitutes.

  • Grouping separators are optional. The character is culture-specific. .NET substitutes. (The positions are also culture-specific but that's only controlled by your format string.) You also use any other character except the special characters for Format (which include , . # 0).

  • 零没有签名。您可以将其显示为任何内容,例如“零”。

  • 小于 1 的绝对值在小数点前有一个前导 0。根据口味调整。

  • 位数用于最大和最小绝对十进制值。根据口味调整。

  • 小数点字符是特定于文化的。.NET 替代品。

  • 分组分隔符是可选的。角色是特定于文化的。.NET 替代品。(这些位置也是特定于文化的,但仅由您的格式字符串控制。)您还可以使用任何其他字符,除了 Format 的特殊字符(包括 , . # 0)。

回答by Peter Kriegel

I cannot comment so i Do an answer here (nuts reputation system ....)

我不能发表评论所以我在这里做一个答案(坚果声誉系统......)

You can Use number.ToString("+0;-#") very well to Produce the UTC String TimeFormat

您可以很好地使用 number.ToString("+0;-#") 来生成 UTC String TimeFormat

Here showed in PowerShell code

这里显示在 PowerShell 代码中

"$([System.DateTime]::Now.ToString('HH:mm:ss.fff'))$($([System.TimeZoneInfo]::Local.GetUtcOffset([System.DateTime]::Now).TotalMinutes.ToString('+0;-#')))"

Thank you @gcores https://stackoverflow.com/users/40256/gcores

谢谢@gcores https://stackoverflow.com/users/40256/gcores