Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C#
I am looking for a program that will write the indian rupees in words like
120.50 in Rupees One Hundreds Twenty and Fifty Paisa Only.
I got the codding but it is in vb. The vb code is here........
Module Module1
    ' Function for conversion of a Indian Rupees into words
    '   Parameter - accept a Currency
    '   Returns the number in words format
    '   You can use this function in Excel, VBA, VB6,.NET
    '====================================================

    '****************************************************
    ' Code Created by Bharat Modha 
    ' Porbandar (Gujarat)-India
    ' Email : bharatmodha@yahoo.com
    '****************************************************

    Function RupeesToWord(ByVal MyNumber)
        Dim Temp
        Dim Rupees, Paisa As String
        Dim DecimalPlace, iCount
        Dim Hundreds, Words As String
        Dim place(9) As String
        place(0) = " Thousand "
        place(2) = " Lakh "
        place(4) = " Crore "
        place(6) = " Arab "
        place(8) = " Kharab "
        On Error Resume Next
        ' Convert MyNumber to a string, trimming extra spaces.
        MyNumber = Trim(Str(MyNumber))
 
        ' Find decimal place.
        DecimalPlace = InStr(MyNumber, ".")
 
        ' If we find decimal place...
        If DecimalPlace > 0 Then
            ' Convert Paisa
            Temp = Left(Mid(MyNumber, DecimalPlace + 1) & "00", 2)
            Paisa = " and " & ConvertTens(Temp) & " Paisa"
 
            ' Strip off paisa from remainder to convert.
            MyNumber = Trim(Left(MyNumber, DecimalPlace - 1))
        End If
 
        '===============================================================
        Dim TM As String  ' If MyNumber between Rs.1 To 99 Only.
        TM = Right(MyNumber, 2)
 
        If Len(MyNumber) > 0 And Len(MyNumber) <= 2 Then
            If Len(TM) = 1 Then
                Words = ConvertDigit(TM)
                RupeesToWord = "Rupees " & Words & Paisa & " Only"
 
                Exit Function
 
            Else
                If Len(TM) = 2 Then
                    Words = ConvertTens(TM)
                    RupeesToWord = "Rupees " & Words & Paisa & " Only"
                    Exit Function
 
                End If
            End If
        End If
        '===============================================================

 
        ' Convert last 3 digits of MyNumber to ruppees in word.
        Hundreds = ConvertHundreds(Right(MyNumber, 3))
        ' Strip off last three digits
        MyNumber = Left(MyNumber, Len(MyNumber) - 3)
 
        iCount = 0
        Do While MyNumber <> ""
            'Strip last two digits
            Temp = Right(MyNumber, 2)
            If Len(MyNumber) = 1 Then
 

                If Trim(Words) = "Thousand" Or _
                Trim(Words) = "Lakh  Thousand" Or _
                Trim(Words) = "Lakh" Or _
                Trim(Words) = "Crore" Or _
                Trim(Words) = "Crore  Lakh  Thousand" Or _
                Trim(Words) = "Arab  Crore  Lakh  Thousand" Or _
                Trim(Words) = "Arab" Or _
                Trim(Words) = "Kharab  Arab  Crore  Lakh  Thousand" Or _
                Trim(Words) = "Kharab" Then
 
                    Words = ConvertDigit(Temp) & place(iCount)
                    MyNumber = Left(MyNumber, Len(MyNumber) - 1)
 
                Else
 
                    Words = ConvertDigit(Temp) & place(iCount) & Words
                    MyNumber = Left(MyNumber, Len(MyNumber) - 1)
 
                End If
            Else
 
                If Trim(Words) = "Thousand" Or _
                   Trim(Words) = "Lakh  Thousand" Or _
                   Trim(Words) = "Lakh" Or _
                   Trim(Words) = "Crore" Or _
                   Trim(Words) = "Crore  Lakh  Thousand" Or _
                   Trim(Words) = "Arab  Crore  Lakh  Thousand" Or _
                   Trim(Words) = "Arab" Then
 

                    Words = ConvertTens(Temp) & place(iCount)
 

                    MyNumber = Left(MyNumber, Len(MyNumber) - 2)
                Else
 
                    '=================================================================
                    ' if only Lakh, Crore, Arab, Kharab

                    If Trim(ConvertTens(Temp) & place(iCount)) = "Lakh" Or _
                       Trim(ConvertTens(Temp) & place(iCount)) = "Crore" Or _
                       Trim(ConvertTens(Temp) & place(iCount)) = "Arab" Then
 
                        Words = Words
                        MyNumber = Left(MyNumber, Len(MyNumber) - 2)
                    Else
                        Words = ConvertTens(Temp) & place(iCount) & Words
                        MyNumber = Left(MyNumber, Len(MyNumber) - 2)
                    End If
 
                End If
            End If
 
            iCount = iCount + 2
        Loop
 
        RupeesToWord = "Rupees " & Words & Hundreds & Paisa & " Only"
    End Function
 
    ' Conversion for hundreds
    '*****************************************
    Private Function ConvertHundreds(ByVal MyNumber)
        Dim Result As String
 
        ' Exit if there is nothing to convert.
        If Val(MyNumber) = 0 Then Exit Function
 
        ' Append leading zeros to number.
        MyNumber = Right("000" & MyNumber, 3)
 
        ' Do we have a hundreds place digit to convert?
        If Left(MyNumber, 1) <> "0" Then
            Result = ConvertDigit(Left(MyNumber, 1)) & " Hundreds "
        End If
 
        ' Do we have a tens place digit to convert?
        If Mid(MyNumber, 2, 1) <> "0" Then
            Result = Result & ConvertTens(Mid(MyNumber, 2))
        Else
            ' If not, then convert the ones place digit.
            Result = Result & ConvertDigit(Mid(MyNumber, 3))
        End If
 
        ConvertHundreds = Trim(Result)
    End Function
 
    ' Conversion for tens
    '*****************************************
    Private Function ConvertTens(ByVal MyTens)
        Dim Result As String
 
        ' Is value between 10 and 19?
        If Val(Left(MyTens, 1)) = 1 Then
            Select Case Val(MyTens)
                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
            ' .. otherwise it's between 20 and 99.
            Select Case Val(Left(MyTens, 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
 
            ' Convert ones place digit.
            Result = Result & ConvertDigit(Right(MyTens, 1))
        End If
 
        ConvertTens = Result
    End Function
 
    Private Function ConvertDigit(ByVal MyDigit)
        Select Case Val(MyDigit)
            Case 1 : ConvertDigit = "One"
            Case 2 : ConvertDigit = "Two"
            Case 3 : ConvertDigit = "Three"
            Case 4 : ConvertDigit = "Four"
            Case 5 : ConvertDigit = "Five"
            Case 6 : ConvertDigit = "Six"
            Case 7 : ConvertDigit = "Seven"
            Case 8 : ConvertDigit = "Eight"
            Case 9 : ConvertDigit = "Nine"
            Case Else : ConvertDigit = ""
        End Select
    End Function
 
End Module
 
I converted the code to c sharp but it showing me error.
plz provide me c# code that I can do my work..
Posted 9-Jun-12 19:27pm
Comments
Sandeep Mewara at 10-Jun-12 1:20am
   
I converted the code to c sharp but it showing me error.
What error? Update question with it.
 
plz provide me c# code that I can do my work..
Asking for code not a good idea. Share whats troubling you and I am sure someone will help.
krumia at 10-Jun-12 4:33am
   
Please post the C# error (compile?, runtime?) along with your C# code. And don't forget to Google the error before posting here. It's a lot faster to Google and find a solution than posting here and waiting for a reply.
Anindya Mukherjee at 19-Mar-13 4:55am
   
THANKS A LOT -ANINDYA MUKHERJEE W.B
Sergey Alexandrovich Kryukov at 22-Jul-14 12:30pm
   
The problem is not explained. The quality of your code is horrific. It's not the problem per se, but reviewing this code would hardly be practical, it would be better to write some new. :-)
—SA
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

The C# code is a bit simpler than that: Converting numbers to the word equivalent. [^]
  Permalink  
Comments
krumia at 10-Jun-12 4:30am
   
I'm sorry the (your) article you pointed would not work. The reason is, our Indian friends have different names for large powers of 10. For example 100,000 is not "hundred-thousand", but "lakh". And 10,000,000 is not "ten million", but "crore". You're article is a fail! :P
OriginalGriff at 10-Jun-12 4:46am
   
It's easy to modify...:laugh:
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

  Permalink  
Comments
Sergey Alexandrovich Kryukov at 22-Jul-14 12:31pm
   
5ed.
—SA
Jαved at 10-Aug-14 5:01am
   
Thank you :)
BillWoodruff at 1-Nov-14 8:24am
   
+5 Bhut achaa hain :)
Jαved at 6-Nov-14 22:18pm
   
Thank you :)
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

Instead of converting the above code I have written the following code, which I think may be helpful for you.
 
In Indian system Kharab and Arab are not generally used in the present time, as I have noticed. Hence, I have not used these terms in the following code
 
void Main()
{
    Console.WriteLine(Utilities.GetNumberAsWords(22001050509109M));
    Console.WriteLine(Utilities.GetNumberAsWords(3452300807M));
    Console.WriteLine(Utilities.GetNumberAsWords(2300807M));
    Console.WriteLine(Utilities.GetNumberAsWords(100000000));
    Console.WriteLine(Utilities.GetNumberAsWords(809584M));
    Console.WriteLine(Utilities.GetNumberAsWords(1000000));
    Console.WriteLine(Utilities.GetNumberAsWords(10937M));
    Console.WriteLine(Utilities.GetNumberAsWords(10005M));
    Console.WriteLine(Utilities.GetNumberAsWords(705M));
    Console.WriteLine(Utilities.GetNumberAsWords(15M));
    Console.WriteLine(Utilities.GetNumberAsWords(1605M));
    Console.WriteLine(Utilities.GetNumberAsWords(105M));
    Console.WriteLine(Utilities.GetNumberAsWords(100M));
    Console.WriteLine(Utilities.GetNumberAsWords(58M));
    Console.WriteLine(Utilities.GetNumberAsWords(17M));
    Console.WriteLine(Utilities.GetNumberAsWords(10M));
    Console.WriteLine(Utilities.GetNumberAsWords(3M));
}
 
public class Utilities {
    private static string[] TensWord = new string[]{string.Empty, string.Empty, "Twenty",
            "Thirty", "Forty", "Fifty", "Sixty", "Seventy", "Eighty", "Ninety"};
    private static string[] OnesWord = new string[] {
            "Zero", "One", "Two", "Three", "Four", "Five", "Six", "Seven",
            "Eight", "Nine", "Ten", "Eleven", "Twelve", "Thirteen", "Fourteen",
            "Fifteen", "Sixteen", "Seventeen", "Eighteen", "Nineteen"};
    private static string[] GroupNames = new string[]{
            "lakh", "thousand",""};
    public static string GetNumberAsWords(decimal number){
        StringBuilder sb = new StringBuilder();
        if (number > 9999999){
            decimal crores = (long)number / 10000000;
            sb.Append(GetNumberAsWords(crores) + (crores == 1 ? " crore" : " crores "));
            number = number % 10000000;
        }
 
        string numberString = number.ToString("00,00,000",
            System.Globalization.CultureInfo.GetCultureInfo("hi-IN"));
        string[] numberParts = numberString.Split(',');
        for(int i=0; i < numberParts.Length; i++){
            if (int.Parse(numberParts[i])==0)
                continue;
            string groupAsWords = groupInWords(numberParts[i]);
            if (sb.Length != 0 && numberParts[i].Length==3 && !groupAsWords.Contains(" and ")){
                sb.Append(" and ");
            }
            sb.Append(groupAsWords + " " + GroupNames[i]);
            sb.Append(numberParts[i].Length == 2 && numberParts[i]!= "01" ? "s " : " ");
        }
        return sb.ToString().Trim();
    }
    private static string groupInWords(string numGroup){
        string groupWords = "";
        if (numGroup.Length==3){
            if (numGroup[0]!='0'){
                int hundreds = int.Parse(numGroup.Substring(0,1));
                groupWords = OnesWord[hundreds] + " hundred ";
                if (int.Parse(numGroup.Substring(1)) != 0)
                    groupWords += "and ";
            }
            numGroup = numGroup.Substring(1);
        }
        int tens = int.Parse(numGroup.Substring(0,1));
        if (tens > 1){
            groupWords += TensWord[tens] + " ";
            numGroup = numGroup.Substring(1);
        }
        int ones = int.Parse(numGroup);
        if (ones > 0)
            groupWords += OnesWord[ones];
        return groupWords;
    }
 
}
//Output
//Twenty Two lakhs One hundred and Five crores Five lakhs Nine thousands One hundred and Nine
//Three hundred and Forty Five crores Twenty Three lakhs Eight hundred and Seven
//Twenty Three lakhs Eight hundred and Seven
//Ten crores
//Eight lakhs Nine thousands Five hundred and Eighty Four
//Ten lakhs
//Ten thousands Nine hundred and Thirty Seven
//Ten thousands  and Five
//Seven hundred and Five
//Fifteen
//One thousand Six hundred and Five
//One hundred and Five
//One hundred
//Fifty Eight
//Seventeen
//Ten
//Three
  Permalink  
Comments
Manas Bhardwaj at 10-Jun-12 14:25pm
   
Good One +5
VJ Reddy at 10-Jun-12 19:37pm
   
Thank you, Manas :)

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

  Print Answers RSS
0 Maciej Los 295
1 OriginalGriff 273
2 Aajmot Sk 234
3 Marcin Kozub 205
4 Richard MacCutchan 200
0 OriginalGriff 7,903
1 Sergey Alexandrovich Kryukov 7,127
2 DamithSL 5,604
3 Manas Bhardwaj 4,986
4 Maciej Los 4,820


Advertise | Privacy | Mobile
Web03 | 2.8.1411023.1 | Last Updated 1 Nov 2014
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100