Tuesday, November 1, 2011

Format numbers with Indian style commas.

In our ERP projects we usually need to display commas in numbers. In most of the format we did not get Indian style commas. So I had developed this class. Its main function is Put_Commas it take string as input and return string with commas.

Public Class ClsCommas
    Public Function Put_Commas(ByVal PassString As String) As String
        Dim IsNegative As Boolean
        Dim DecimalPart As String
        Dim TLen As Long            'Length of String without "-" and "."
        Dim LString As String       'String for Storing Changed String
        '-------
        'Check Must Be Greater than 999
        '------

        If System.Math.Abs(Decimal.Parse(PassString)) <= 999 Then
            Put_Commas = PassString
            Exit Function
        End If

        '-------------
        'Check Negative Values
        '-------------
        If Decimal.Parse(PassString) < 0 Then
            IsNegative = True
            PassString = Right(PassString, Len(PassString) - 1)
        Else
            IsNegative = False
        End If
        '---------
        'Remove Decimal Part
        '---------

        If PassString.Contains(".") Then
            DecimalPart = Right(PassString, Len(PassString) - PassString.IndexOf("."))
            PassString = Left(PassString, InStr(1, PassString, ".") - 1)
        Else
            DecimalPart = ""
        End If
        '-----------------
        TLen = Len(PassString)
        If TLen > 3 Then            'First Time Three Char
            LString = Right(PassString, 3)
            TLen = TLen - 3
            PassString = Left(PassString, Len(PassString) - 3)
        Else
            LString = ""

        End If
        Do While TLen > 2
            LString = Right(PassString, 2) + "," + LString
            TLen = TLen - 2
            PassString = Left(PassString, Len(PassString) - 2)

        Loop
        LString = Trim(PassString) + "," + Trim(LString)
        '--------------
        'Add Decimal Part
        '-------------
        If Trim(DecimalPart) <> "" Then
            If Trim(DecimalPart.Contains(".")) = False Then
                LString = LString + "." + DecimalPart
            Else
                LString = LString + DecimalPart
            End If
            '-------------------------------------------------
        End If
        '-----------
        'Check for Negative
        '-----------
        If IsNegative Then
            LString = "-" + Trim(LString)
        End If
        Put_Commas = LString
    End Function
    Private Function Right(ByVal PassStr As String, ByVal RChar As Integer) As String
        Dim LStr As String
        Try
            LStr = PassStr.Substring(PassStr.Length - RChar, RChar)
            Return LStr
        Catch ex As Exception
            Return PassStr
        End Try
    End Function
    Private Function Left(ByVal PassStr As String, ByVal LChar As Integer) As String
        Dim LStr As String
        Try
            LStr = PassStr.Substring(0, LChar)
            Return LStr
        Catch ex As Exception
            Return PassStr
        End Try
    End Function
End Class

Happy Coding!!
Tariq


No comments:

Post a Comment