Blog

Convert amount into words (Rupees)

Posted by Rizwan Ahmed Blogs | VB.NET Tutorials Apr 19, 2011
This module converts the amount into words.Here if you enter some amount say 123.45 than you will get "One Hundereds Twenty Three Rupees and Forty Five Paise". This will give you result for maximun of (9,2) number
Download Files: RsInWords.zip
Code for IndianRupee.aspx.VB


Partial Class IndianRupee
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

    End Sub
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Label1.Text = Spell(TextBox1.Text)
    End Sub

    ' Converts a number from 100-999 into text
    Function GetHundreds(ByVal MyNumber As String) As String
        Dim Result As String
        If Val(MyNumber) = 0 Then : Return "" : Exit Function : End If
        MyNumber = Microsoft.VisualBasic.Strings.Right("000" & MyNumber, 3)
        ' Convert the hundreds place.
        If Mid(MyNumber, 1, 1) <> "0" Then
            Result = GetDigit(Mid(MyNumber, 1, 1)) & " Hundred "
        End If
        ' Convert the tens and ones place.
        If Mid(MyNumber, 2, 1) <> "0" Then
            Result = Result & GetTens(Mid(MyNumber, 2))
        Else
            Result = Result & GetDigit(Mid(MyNumber, 3))
        End If
        GetHundreds = Result
    End Function
    ' Converts a number from 10 to 99 into text.
    Function GetTens(ByVal TensText As String) As String
        Dim Result As String
        Result = ""           ' Null out the temporary function value.
        If Val(Microsoft.VisualBasic.Strings.Left(TensText, 1)) = 1 Then   ' If value between 10-19...
            Select Case Val(TensText)
                Case 10 : Result = "Ten"
                Case 11 : Result = "Eleven"
                Case 12 : Result = "Twelve"
                Case 13 : Result = "Thirteen"
                Case 14 : Result = "Fourteen"
                Case 15 : Result = "Fifteen"
                Case 16 : Result = "Sixteen"
                Case 17 : Result = "Seventeen"
                Case 18 : Result = "Eighteen"
                Case 19 : Result = "Nineteen"
                Case Else
            End Select
        Else                                 ' If value between 20-99...
            Select Case Val(Microsoft.VisualBasic.Strings.Left(TensText, 1))
                Case 2 : Result = "Twenty "
                Case 3 : Result = "Thirty "
                Case 4 : Result = "Forty "
                Case 5 : Result = "Fifty "
                Case 6 : Result = "Sixty "
                Case 7 : Result = "Seventy "
                Case 8 : Result = "Eighty "
                Case 9 : Result = "Ninety "
                Case Else
            End Select
            Result = Result & GetDigit(Microsoft.VisualBasic.Strings.Right(TensText, 1))  ' Retrieve ones place.
        End If
        GetTens = Result
    End Function
    ' Converts a number from 1 to 9 into text.
    Function GetDigit(ByVal Digit As String) As String
        Select Case Val(Digit)
            Case 1 : GetDigit = "One"
            Case 2 : GetDigit = "Two"
            Case 3 : GetDigit = "Three"
            Case 4 : GetDigit = "Four"
            Case 5 : GetDigit = "Five"
            Case 6 : GetDigit = "Six"
            Case 7 : GetDigit = "Seven"
            Case 8 : GetDigit = "Eight"
            Case 9 : GetDigit = "Nine"
            Case Else : GetDigit = ""
        End Select
    End Function

    Public Function Spell(ByVal Num As String) As String
        Dim Total, DecimalPlace, Count As Integer
        Dim Temp As String = ""
        Dim Rupees As String = ""
        Dim Paise As String = ""
        Dim Place(9) As String
        Num = Trim(Num)
        DecimalPlace = InStr(Num, ".")
        If DecimalPlace > 0 Then
            Paise = GetTens(Microsoft.VisualBasic.Strings.Left(Mid(Num, DecimalPlace + 1) & "00", 2).ToString)
            Num = Trim(Microsoft.VisualBasic.Strings.Left(Num, DecimalPlace - 1))
        End If
        Count = 1
        Total = Num.Length
        Do
            If Total >= 8 And Total <= 9 Then
                If Total = 9 Then
                    Temp = GetTens(Num.Substring(0, 2))
                    If Temp <> "" Then Rupees = Rupees & Temp & " Carores "
                    Num = Num.Remove(0, 2)
                Else
                    Temp = GetDigit(Num.Substring(0, 1))
                    If Temp <> "" Then Rupees = Rupees & Temp & " Carore "
                    Num = Num.Remove(0, 1)
                End If

                ''''''''''''''''''''''''''''''''''''''''
            ElseIf Total >= 6 And Total <= 7 Then
                If Total = 7 Then
                    Temp = GetTens(Num.Substring(0, 2))
                    If Temp <> "" Then Rupees = Rupees & Temp & " Lakhs "
                    Num = Num.Remove(0, 2)
                Else
                    Temp = GetDigit(Num.Substring(0, 1))
                    If Temp <> "" Then Rupees = Rupees & Temp & " Lakh "
                    Num = Num.Remove(0, 1)
                End If


                '''''''''''''''''''''''''''''''''''''''''
            ElseIf Total >= 4 And Total <= 5 Then
                If Total = 5 Then
                    Temp = GetTens(Num.Substring(0, 2))
                    If Temp <> "" Then Rupees = Rupees & Temp & " Thousands "
                    Num = Num.Remove(0, 2)
                Else
                    Temp = GetDigit(Num.Substring(0, 1))
                    If Temp <> "" Then Rupees = Rupees & Temp & " Thousand "
                    Num = Num.Remove(0, 1)
                End If
                ''''''''''''''''''''''''''''''''''''''''''
            ElseIf Total = 3 Then

                Temp = GetDigit(Num.Substring(0, 1))
                If Temp <> "" Then Rupees = Rupees & Temp & " Hundereds "
                Num = Num.Remove(0, 1)

                ''''''''''''''''''''''''''''''''''''''''''
            ElseIf Total = 2 Then

                Temp = GetTens(Num.Substring(0, 2))
                If Temp <> "" Then Rupees = Rupees & Temp
                Num = Num.Remove(0, 2)
                '''''''''''''''''''''''''''''''''''''''''''
            ElseIf Total = 1 Then

                Temp = GetDigit(Num.Substring(0, 1))
                If Temp <> "" Then Rupees = Rupees & Temp
                Num = Num.Remove(0, 1)
            Else
                Label1.Text = "Less Than 10 carore"

                Exit Function
            End If

            Total = Num.Length
        Loop While Num.Length > 0

        Select Case Rupees
            Case ""
                Rupees = "No Rupee"
            Case "One"
                Rupees = "One Rupees"
            Case Else
                Rupees = Rupees & " Rupees"
        End Select
        Select Case Paise
            Case ""
                Paise = " and No Paise"
            Case "One"
                Paise = " and One Paise"
            Case Else
                Paise = " and " & Paise & " Paise"
        End Select

        Spell = Rupees & Paise
    End Function
 
End Class



Code For IndianRupee.aspx


<%@ Page Language="VB" AutoEventWireup="false" CodeFile="IndianRupee.aspx.vb" Inherits="IndianRupee" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>Get Rupees in Words</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <center>
    <table width="700px" border ="0">
    <tr><td style="width: 140px">
        Amount in Digits (9,2)</td>
    <td><asp:TextBox ID="TextBox1" runat="server" MaxLength="12"></asp:TextBox></td>
   
      </tr>
      <tr><td style="width: 150px">     &nbsp;</td>
        <td align="left">
       
        <asp:Button ID="Button1" runat="server" Text="Button" /></td>
        </tr>
        <tr>
            <td style="width: 150px">
                Amount in Words</td>
            <td>
                <asp:Label ID="Label1" runat="server"></asp:Label></td>
        </tr>
        </table>
        </center>
        </div>
    </form>
</body>
</html>

COMMENT USING
PREMIUM SPONSORS
Hire Mobile & Web Developer on demand. 100% satisfaction. Try for 1 week or Money Back. Local and remote developers available all over USA.
More Blogs from this Blogger
SPONSORED BY
  • MCN is your source for developing solutions involving websites, mobile apps, cloud-computing, databases, BI, back-end services and processes and client-server applications.
Employers - Post Free Jobs