Click here to Skip to main content
15,886,199 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi All,

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

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


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
Posted
Updated 17-Jul-20 8:32am
Comments
PIEBALDconsult 22-Feb-15 12:39pm    
You shouldn't be using strings for numbers.

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
VB
row.Item("NewQty") = CType(row.Item("TQTY"), decimal).ToString("G")
 
Share this answer
 
v2
I made the below extension methods for myself to fit one of my projects, but maybe they'll be beneficial to someone else.

VB
Imports System.Numerics
Imports System.Text.RegularExpressions
Imports System.Runtime.CompilerServices

Friend Module ExtensionMethod
    <Extension()>
    Friend Function TrimDecimal(ByVal obj As BigInteger) As String
        Return obj.ToString().TrimDecimal()
    End Function

    <Extension()>
    Friend Function TrimDecimal(ByVal obj As Decimal) As String
        Return New BigInteger(obj).ToString().TrimDecimal()
    End Function

    <Extension()>
    Friend Function TrimDecimal(ByVal obj As Double) As String
        Return New BigInteger(obj).ToString().TrimDecimal()
    End Function

    <Extension()>
    Friend Function TrimDecimal(ByVal obj As Single) As String
        Return New BigInteger(obj).ToString().TrimDecimal()
    End Function

    <Extension()>
    Friend Function TrimDecimal(ByVal obj As String) As String
        If String.IsNullOrWhiteSpace(obj) OrElse Not Regex.IsMatch(obj, "^(\d+([.]\d*)?|[.]\d*)$") Then Return String.Empty
        Dim rex As Regex = New Regex("^[0]*(?<pre>([0-9]+)?)(?<post>([.][0-9]*)?)$")
        Dim mev As MatchEvaluator = Function(m) String.Concat(If(m.Groups("pre").Length > 0, m.Groups("pre").Value, "0"), m.Groups("post").Value.TrimEnd({"."c, "0"c}))
        Return rex.Replace(obj, mev)
    End Function
End Module


Though it will require a reference to System.Numerics.
 
Share this answer
 
Solved it. If for use for anyone, I have created the following common function:

VB
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.
 
Share this answer
 
Comments
KP Ghori 15-Jun-15 6:20am    
The above function not work properly
if i Enter 03.00 then it return 03
this is not proper answer.
atul sharma 5126 16-Jun-15 1:33am    
If you also want to remove preceding zeroes as well then pls use this updated function:

Public Function fnNumberwithoutzero(ByRef Value As String)
fnNumberwithoutzero = 0

Dim parts() As String = Value.Split("."c)

'for removing preceding zeros
Dim firsthalf As Object = parts(0).ToString.TrimStart("0".ToCharArray)

'for removing trailing zeros
If parts.Length = 2 Then

Dim secondhalf As Object = parts(1).ToString.TrimEnd("0".ToCharArray)

If secondhalf = "" Then
fnNumberwithoutzero = firsthalf
Else
fnNumberwithoutzero = firsthalf & "." & secondhalf
End If
Else
fnNumberwithoutzero = firsthalf
End If

Return fnNumberwithoutzero
End Function

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900