如何仅在小数点后删除尾随零

编程入门 行业动态 更新时间:2024-10-24 00:25:10
本文介绍了如何仅在小数点后删除尾随零的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

大家好, 我正在使用以下代码来删除网格中字段的小数点后的零:

Hi All, I am using following code to remove trailing zeros after decimal for a field in grid:

dtBOMDetails.Columns.Add("NewQty") For Each row As DataRow In dtBOMDetails.Rows row.Item("NewQty") = row.Item("TQTY").ToString.TrimEnd(".0".ToCharArray) Next

问题在于它还会将100转换为1,即即使十进制前也将最后删除零. 请提出建议. 我想要以下结果: 9.23000 = 9.23 90.00000 = 90 谢谢

The problem is that it also converts 100 to 1 i.e. zeros in the end are also removed even if before decimal. Pls suggest. I want following results: 9.23000 = 9.23 90.00000 = 90 Thanks

推荐答案

为什么不先将数字转换为数字格式,然后再使用ToString以所需的方式对其进行格式化.参见 - Decimal.ToString方法(字符串) [ ^ ] -标准数字格式字符串 [ ^ ] -自定义数字格式字符串 [ ^ ] 例如 Why not convert the number to a numeric format first and then use ToString to format it in a desired way. See - Decimal.ToString Method (String)[^] - Standard Numeric Format Strings[^] - Custom Numeric Format Strings[^] For example row.Item("NewQty") = CType(row.Item("TQTY"), decimal).ToString("G")

已解决.如果供任何人使用,我创建了以下常用功能: Solved it. If for use for anyone, I have created the following common function: Public Function fnNumberwithoutzero(ByRef Value As String) fnNumberwithoutzero = 0 Dim parts() As String = Value.Split("."c) If parts.Length = 2 Then Dim firsthalf As Object = parts(0) Dim secondhalf As Object = parts(1).ToString.TrimEnd("0".ToCharArray) If secondhalf = "" Then fnNumberwithoutzero = firsthalf Else fnNumberwithoutzero = firsthalf & "." & secondhalf End If End If Return fnNumberwithoutzero End Function

希望其他人觉得有帮助.

Hope others find it helpful.

更多推荐

如何仅在小数点后删除尾随零

本文发布于:2023-10-14 22:28:15,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1492401.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:小数点

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!