[英]string.Format() Blank Zero
在我的應用程序中,可以使用string.Format()
函數來格式化字符串。 我想添加當結果為零時返回空白的可能性。
據我所知,可以使用以下代碼來做到這一點: 0.toString("0;; ");
,但正如我已經提到的,我需要使用string.Format()
函數(因為它必須能夠使用例如{0:P}
格式的百分比。
有誰知道如何使用string.Format()
函數將零值清空?
String.Format()
支持;
節分隔符。
嘗試例如String.Format("{0:#%;;' '}", 0);
.
我的回答有點晚了,但您可能想嘗試以下操作:
{0:#.##%;-#.##%;''}
為什么不用if else
語句呢?
string result = String.Format(the value);
if(result=="0")
{
result=" ";
}
或者,也許更優雅地作為int
的擴展方法:
public static class StringFormatters
{
public static string ToNonZeroString(this int i) => i == 0 ? "" : i.ToString();
}
然后
(1+1-2).ToNonZeroString()
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.