使用.NET字符串格式化,我怎么格式化字符串来显示空白(空字符串),零(0)? [英] Using .NET string formatting, how do I format a string to display blank (empty string) for zero (0)?

查看:217
本文介绍了使用.NET字符串格式化,我怎么格式化字符串来显示空白(空字符串),零(0)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的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)

这篇关于使用.NET字符串格式化,我怎么格式化字符串来显示空白(空字符串),零(0)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆