使用.NET字符串格式化,我怎么格式化字符串来显示空白(空字符串),零(0)?字符串、空白、我怎么、空字符串

2023-09-03 16:34:15 作者:从齐耳短发到长发及腰

我使用的DataBinder.Eval EX pression在ASP.NET Datagrid的,但我认为这个问题适用于字符串格式化在.NET中一般。有客户请求,如果字符串的值是0,它不应该被显示。我有下面的技巧来实现:

I am using a DataBinder.Eval expression in an ASP.NET Datagrid, but I think this question applies to String formatting in .NET in general. The customer has requested that if the value of a string is 0, it should not be displayed. I have the following hack to accomplish this:

<%# IIf(DataBinder.Eval(Container.DataItem, "MSDWhole").Trim = "0", "", 
    DataBinder.Eval(Container.DataItem, "MSDWhole", "{0:N0}"))  %>

我想修改 {0:N0} 格式化EX pression,这样我可以消除IIF语句,但无法找到任何工作

I would like to change the {0:N0} formatting expression so that I can eliminate the IIf statement, but can't find anything that works.

推荐答案

您需要使用的部分分离,像这样的:

You need to use the section separator, like this:

<%# DataBinder.Eval(Container.DataItem, "MSDWhole", "{0:N0;; }").Trim() %>

请注意,只有消极的部分可以是空的,所以我需要把一个空间,在 0 部分。 (阅读文档)

Note that only the negative section can be empty, so I need to put a space in the 0 section. (Read the documentation)