如何仅在小数点后删除尾随零 [英] How Do I Remove Trailing Zeros Only After Decimals

查看:128
本文介绍了如何仅在小数点后删除尾随零的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

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

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.


这篇关于如何仅在小数点后删除尾随零的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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