是否有任何方法我可以指定一个标准或自定义数字格式字符串总是输出符号,是+ve或-ve(尽管它应该为零做什么,我不确定!)
当前回答
注意,当使用条件格式时,负数不会自动获得符号。你需要做的
string MyString = number.ToString("+#;-#;0");
其他回答
注意,当使用条件格式时,负数不会自动获得符号。你需要做的
string MyString = number.ToString("+#;-#;0");
我不能评论,所以我在这里做一个回答(坚果声誉系统....)
你可以使用number.ToString("+0;-#")很好地产生UTC字符串时间格式
这里显示在PowerShell代码中
"$([System.DateTime]::Now.ToString('HH:mm:ss.fff'))$($([System.TimeZoneInfo]::Local.GetUtcOffset([System.DateTime]::Now).TotalMinutes.ToString('+0;-#')))"
谢谢@gcores https://stackoverflow.com/users/40256/gcores
对于任何类型的数值表达式:
+###,###,###,###,###,###,###,###,###,##0.###,###,###,###,###,###,###,###,###,###;-###,###,###,###,###,###,###,###,###,##0.###,###,###,###,###,###,###,###,###,###;0
在三种情况下使用三个部分:阳性;阴性;零
这个例子的其他方面:
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).
你也可以在string.Format();格式字符串用冒号(':')与索引分隔。
var f = string.Format("{0}, Force sign {0:+#;-#;+0}, No sign for zero {0:+#;-#;0}", number);
对于数字{+1,-1,0},得到:
1,力符号+1,0 +1没有符号 -1,强制符号-1,零没有符号-1 0,强制符号+0,0没有符号
也可以使用插值字符串来代替字符串。格式以获得相同的结果:
var f = $"{number}, Force sign {number:+#;-#;+0}, No sign for zero {number:+#;-#;0}";
是的,你可以。 有条件格式。参见MSDN中的条件格式
eg:
string MyString = number.ToString("+0;-#");
用分号分隔的每个部分代表正数和负数
or:
string MyString = number.ToString("+#;-#;0");
如果你不想让0带正号。
推荐文章
- 实体框架核心:在上一个操作完成之前,在此上下文中开始的第二个操作
- 如何为构造函数定制Visual Studio的私有字段生成快捷方式?
- 为什么Visual Studio 2015/2017/2019测试运行器没有发现我的xUnit v2测试
- 如何使用JSON确保字符串是有效的JSON。网
- AppSettings从.config文件中获取值
- 通过HttpClient向REST API发布一个空体
- 如何检查IEnumerable是否为空或空?
- 自动化invokerrequired代码模式
- 没有ListBox。SelectionMode="None",是否有其他方法禁用列表框中的选择?
- 在c#代码中设置WPF文本框的背景颜色
- 在c#中,什么是单子?
- c#和Java中的泛型有什么不同?和模板在c++ ?
- c#线程安全快速(est)计数器
- 如何将此foreach代码转换为Parallel.ForEach?
- 如何在iis7应用程序池中设置。net Framework 4.5版本