修剪小数点中的零

发布于 2024-10-29 19:08:24 字数 203 浏览 4 评论 0原文

如果多余的零没有任何小数值,我想删除它们。但如果他们有的话,我想向他们展示。 在此处输入图像描述

在图片中,我想从 10.00 和 20.00 中提高不必要的 0。但我想显示下面的其他 3 条记录。

我使用c#、asp.net 4.0和GridView来显示.

I would like to remove the extra zeros if they don't have any decimal values. But if they have, I would like to show them.
enter image description here

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 .

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

儭儭莪哋寶赑 2024-11-05 19:08:24

尝试

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

Try

<asp:BoundField DataField="Weight" DataFormatString="{0:#.##}" />
眼趣 2024-11-05 19:08:24

如果您对格式有疑问 Kathy Kam 的 .NET 格式字符串 101 是您的终极资源。

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

聚集的泪 2024-11-05 19:08:24

我看不到图片,但听起来您只需要一个格式字符串:如果需要,# 字符代表一个数字,0 字符代表您总会有一个数字。因此 "#.##" 的格式字符串听起来可能就是您所需要的。

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.

与他有关 2024-11-05 19:08:24

我相信“#”自定义格式字符可能适合您。查看 MSDN 上的“自定义数字格式字符串”:

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

您需要在 GridView 配置中的某处指定列的“格式”。

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.

天涯离梦残月幽梦 2024-11-05 19:08:24

你总是可以按照程序员的方式去做。

定义一个作为标签的 TemplateField,其 Text 属性的绑定如下:Text='<%# WeightText( Container.DataItem ) %>'

在后面的代码中定义 WeightText 方法,该方法将 Container.DataItem 作为参数。

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)
}

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)
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文