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}")) %>
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.
The “0” custom specifier is a zero placeholder. If the value to be formatted has a digit in the position where the zero appears in the format string, the the digit is copied to the resultant string.
The string format() method formats the given string into a nicer output in Python. The syntax of the format() method is: template. format(p0, p1, ..., k0=v0, k1=v1, ...)
You need to use the section separator, like this:
<%# DataBinder.Eval(Container.DataItem, "MSDWhole", "{0:N0;; }").Trim() %>
Note that only the negative section can be empty, so I need to put a space in the 0
section. (Read the documentation)
Given the accepted answer:
<%# DataBinder.Eval(Container.DataItem, "MSDWhole", "{0:N0;; }").Trim() %>
The a space is placed in the the 3rd position, however placing a #
in the third position will eliminate the need to call Trim()
.
<%# DataBinder.Eval(Container.DataItem, "MSDWhole", "{0:N0;;#}") %>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With