开发者

Trim zeros in the decimal

I would like to remove the extra zeros if they don't have any decimal values. 开发者_JAVA百科But if they have, I would like to show them.

Trim zeros in the decimal

In the picture, I want to height the unnecessary 0 from the 10.00 and 20.00. But I want to show the other 3 records below.

I used c#, asp.net 4.0 and GridView to display .


Try

<asp:BoundField DataField="Weight" DataFormatString="{0:#.##}" />


If you have trouble with formats Kathy Kam's .NET Format String 101 is ultimate resource for you.


I can't see the picture, but it sounds like you just need a format string: The # character represents a digit if it's needed, and a 0 character represents that you'll always have a digit. So a format string of "#.##" sounds like it might be what you need.


I believe the "#" custom format character might work for you. Take a look at the "Custom Numeric Format Strings" on MSDN:

http://msdn.microsoft.com/en-us/library/0c899ak8.aspx

You will need to specify the "Format" for the column somewhere in your GridView configuration.


You could always go at it the programmers way.

Define a TemplateField that is a label and its Text property is bound like so: Text='<%# WeightText( Container.DataItem ) %>'.

Define the WeightText method in the code behind that takes the Container.DataItem as a parameter.

protected string WeightText(object dataItem)
{
   // cast it to your object
   MyObject myObject = (MyObject)dataItem;
   decimal d = (decimal)myObject.Weight;
   decimal fraction = d - decimal.Remainder(d);
   if(fraction > 0)
       return d.ToString(); // string with fraction (format this however you want)
   else
       return decimal.Remainder(d).ToString(); // string without fraction (format this however you want)
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜