VBcoders Guest



Don't have an account yet? Register
 


Forgot Password?



Convert Number to Indian Rupees

by Anandh Kumar. B (1 Submission)
Category: String Manipulation
Compatability: Visual Basic 3.0
Difficulty: Unknown Difficulty
Originally Published: Tue 9th June 2009
Date Added: Mon 8th February 2021
Rating: (1 Votes)

Convert Number to Indian Rupees

API Declarations


Msgbox CRupees(123456.78)

Rate Convert Number to Indian Rupees



    On Error GoTo 0
    Const PREC_NONE = 11, PREC_UNARY = 10, PREC_POWER = 9, PREC_TIMES = 8, PREC_DIV = 7, PREC_INT_DIV = 6, PREC_MOD = 5, PREC_PLUS = 4
    Dim IsUnary As Boolean, NextUnary As Boolean
    Dim Parenthesis As Integer, pos As Integer, ExpLen As Integer, HigherPos As Integer, HigherPREC As Integer
    Dim LeftExp As String, RightExp As String, Value As String, ch As String

    Expression = Trim$(Expression)
    ExpLen = Len(Expression)
    If ExpLen = 0 Then Exit Function

    IsUnary = True
    HigherPREC = PREC_NONE
    
    For pos = 1 To ExpLen
        ch = Mid$(Expression, pos, 1)
        NextUnary = False
        
        If ch = " " Then
            NextUnary = IsUnary
        ElseIf ch = "(" Then
            Parenthesis = Parenthesis + 1
            NextUnary = True
        ElseIf ch = ")" Then
            Parenthesis = Parenthesis - 1
            NextUnary = True
        ElseIf Parenthesis = 0 Then
            If ch = "^" Or ch = "*" Or ch = "/" Or ch = "\" Or ch = "%" Or ch = "+" Or ch = "-" Then
                NextUnary = True
                Select Case ch
                    Case "^"
                        If HigherPREC >= PREC_POWER Then
                            HigherPREC = PREC_POWER
                            HigherPos = pos
                        End If
                    Case "*", "/"
                        If HigherPREC >= PREC_TIMES Then
                            HigherPREC = PREC_TIMES
                            HigherPos = pos
                        End If
                    Case "\"
                        If HigherPREC >= PREC_INT_DIV Then
                            HigherPREC = PREC_INT_DIV
                            HigherPos = pos
                        End If
                    Case "%"
                        If HigherPREC >= PREC_MOD Then
                            HigherPREC = PREC_MOD
                            HigherPos = pos
                        End If
                    Case "+", "-"
                        If (Not IsUnary) And _
                            HigherPREC >= PREC_PLUS _
                        Then
                            HigherPREC = PREC_PLUS
                            HigherPos = pos
                        End If
                End Select
            End If
        End If
        IsUnary = NextUnary
    Next pos
    
    If HigherPREC < PREC_NONE Then
        LeftExp = Left$(Expression, HigherPos - 1)
        RightExp = Right$(Expression, ExpLen - HigherPos)
        Select Case Mid$(Expression, HigherPos, 1)
            Case "^"
                Calculator = _
                    Calculator(LeftExp) ^ _
                    Calculator(RightExp)
            Case "*"
                Calculator = _
                    Calculator(LeftExp) * _
                    Calculator(RightExp)
            Case "/"
                Calculator = _
                    Calculator(LeftExp) / _
                    Calculator(RightExp)
            Case "\"
                Calculator = _
                    Calculator(LeftExp) \ _
                    Calculator(RightExp)
            Case "%"
                Calculator = _
                    Calculator(LeftExp) Mod _
                    Calculator(RightExp)
            Case "+"
                Calculator = _
                    Calculator(LeftExp) + _
                    Calculator(RightExp)
            Case "-"
                Calculator = _
                    Calculator(LeftExp) - _
                    Calculator(RightExp)
        End Select
        Exit Function
    End If
    
    If Left$(Expression, 1) = "(" And Right$(Expression, 1) = ")" Then
        Calculator = Calculator(Mid$(Expression, 2, ExpLen - 2))
        Exit Function
    End If
        
    If Left$(Expression, 1) = "-" Then
        Calculator = -Calculator( _
            Right$(Expression, ExpLen - 1))
        Exit Function
    End If
    
    If Left$(Expression, 1) = "+" Then
        Calculator = Calculator( _
            Right$(Expression, ExpLen - 1))
        Exit Function
    End If
    
    Calculator = Val(Expression)
End Function

Public Function CRupees(ByVal num As Double, Optional RSPFix As String = "", Optional PSPFix As String = " and ", Optional PSSFix As String = " Paisa")
    Dim Rupees As String, NumStr As String
    Dim DecimalPlace As Byte
    Dim NumVal As Double
    
    DecimalPlace = Val(InStr(num, "."))
    If DecimalPlace > 0 Then
        NumStr = Format(num, "0.00")
        NumVal = Val(Left(NumStr, DecimalPlace - 1))
        Rupees = CWord(NumVal)
        Rupees = Rupees & PSPFix & CWord(Val(Mid(NumStr, InStr(NumStr, ".") + 1, Len(NumStr)))) & PSSFix
    Else
        NumStr = Trim$(Str$(num))
        NumVal = Val(NumStr)
        Rupees = CWord(NumVal)
    End If
        
    CRupees = IIf(Rupees = "", "", RSPFix & Rupees)
End Function
Public Function CWord(ByVal num As Double)
    Dim WordPad As Variant, ElementPad As Variant
    Dim Word As String, NumStr As String
    Dim NumLen As Byte, CurPos As Byte
    Dim pos As Integer
    
    NumStr = Trim$(Str$(num))
    NumLen = Len(NumStr): pos = 0: Word = ""
    ElementPad = Array("", "One ", "Two ", "Three ", "Four ", "Five ", "Six ", "Seven ", "Eight ", "Nine ", "Ten ", _
                           "Eleven ", "Twelve ", "Thirteen ", "Fourteen ", "Fifteen ", "Sixteen ", "Seventeen ", "Eighteen ", "Nineteen ", _
                           "Twenty ", "Thirty ", "Forty ", "Fifty ", "Sixty ", "Seventy ", "Eighty ", "Ninety ")
    WordPad = Array("", "", "", "Hundred ", "Thousand ", "Thousand ", "Lakhs ", "Lakhs ", "Crores ", "Crores ")
    
    While (pos < NumLen)
        CurPos = NumLen - pos
        Select Case CurPos
            Case 3
                Word = Word + IIf(Val(Mid(NumStr, pos + 1, 1)) > 0, ElementPad(Val(Mid(NumStr, pos + 1, 1))) + WordPad(CurPos), "")
                pos = pos + 1
            Case Else
                If (CurPos Mod 2 = 0) And (CurPos > 2) Then
                    Word = Word + ElementPad(Val(Mid(NumStr, pos + 1, 1))) + WordPad(CurPos)
                    pos = pos + 1
                Else
                    If Val(Mid(NumStr, pos + 1, 2)) > 19 Then
                        Word = Word + ElementPad(Int(Mid(NumStr, pos + 1, 2) / 10) + 18) + ElementPad(Val(Mid(NumStr, pos + 1, 2)) Mod 10) + WordPad(CurPos)
                    ElseIf Val(Mid(NumStr, pos + 1, 2)) > 0 Then
                        Word = Word + ElementPad(Val(Mid(NumStr, pos + 1, 2))) + WordPad(CurPos)
                    End If
                    
                    pos = pos + 2
                End If
        End Select
    Wend
    
    CWord = Trim$(Word)
End Function


Download this snippet    Add to My Saved Code

Convert Number to Indian Rupees Comments

No comments have been posted about Convert Number to Indian Rupees. Why not be the first to post a comment about Convert Number to Indian Rupees.

Post your comment

Subject:
Message:
0/1000 characters