Click here to Skip to main content
15,881,757 members
Articles / Programming Languages / Visual Basic

Numbers to Indian (Hindi) Words Conversion In Unicode

Rate me:
Please Sign up or sign in to vote.
4.85/5 (9 votes)
29 Dec 2011CPOL3 min read 68K   2.6K   11   25
In Unicode, Number to Indian (Hindi) Words Conversion

Declaration

I am not an Indian and conversion I made below is based on the knowledge I gained after reading and understanding how the Hindi (Indian) numbering system works. So, please leave comments where improvements are required. Thanks!

NumbersToHindiWords.png 

Introduction

I recently, post an article to convert numbers to words and then I realize that the same logic can be used to convert any other language so here I am starting with Hindi (Indian) language but using Unicode to get the converted numbers in Native language. As you know, the benefit of Unicode, you don’t have to install the font and can display the Native language in web pages as well.
The entire conversion relies on these three arrays. Now, similar arrays can be developed for another languages and conversion can be made. Granted you have to understand how the numbering logic works for that language. Indonesian and Chinese are coming soon!

VB.NET
Private HundredHindiDigitArray() = _
    {"", "एक", "दो", "तीन", "चार", "पाँच", "छह", "सात", "आठ", "नौ", "दस", _
    "ग्यारह", "बारह", "तेरह", "चौदह", "पन्द्रह", "सोलह", "सत्रह", "अठारह", "उन्नीस", "बीस", _
    "इक्कीस", "बाईस", "तेईस", "चौबीस", "पच्चीस", "छब्बीस", "सत्ताईस", "अट्ठाईस", "उनतीस", "तीस", _
    "इकतीस", "बत्तीस", "तैंतीस", "चौंतीस", "पैंतीस", "छत्तीस", "सैंतीस", "अड़तीस", "उनतालीस", "चालीस", _
    "इकतालीस", "बयालीस", "तैंतालीस", "चौवालीस", "पैंतालीस", "छियालीस", "सैंतालीस", "अड़तालीस", "उनचास", "पचास", _
    "इक्यावन", "बावन", "तिरेपन", "चौवन", "पचपन", "छप्पन", "सत्तावन", "अट्ठावन", "उनसठ", "साठ", _
    "इकसठ", "बासठ", "तिरेसठ", "चौंसठ", "पैंसठ", "छियासठ", "सड़सठ", "अड़सठ", "उनहत्तर", "सत्तर", _
    "इकहत्तर", "बहत्तर", "तिहत्तर", "चौहत्तर", "पचहत्तर", "छिहत्तर", "सतहत्तर", "अठहत्तर", "उनासी", "अस्सी", _
    "इक्यासी", "बयासी", "तिरासी", "चौरासी", "पचासी", "छियासी", "सत्तासी", "अट्ठासी", "नवासी", "नब्बे", _
    "इक्यानबे", "बानबे", "तिरानबे", "चौरानबे", "पंचानबे", "छियानबे", "सत्तानबे", "अट्ठानबे", "निन्यानबे"}

  Private HigherDigitHindiNumberArray() = {"", "", "सौ", "हजार", "लाख", "करोड़", "अरब", "खरब", "नील"}
  Private HigherDigitSouthAsianStringArray() As String = {"", "", "Hundred", "Thousand", "Lakh", "Karod", _
                                                         "Arab", "Kharab", "Neel"}

  Private SouthAsianCodeArray() As String = {"1", "22", "3", "4", "42", "5", "52", "6", "62", "7", "72", _
                                             "8", "82", "9", "92"}
  Private EnglishCodeArray() As String = {"1", "22", "3"}

  Private SingleDigitStringArray() As String = {"", "One", "Two", "Three", "Four", "Five", "Six", "Seven", _
                                                "Eight", "Nine", "Ten"}
  Private DoubleDigitsStringArray() As String = {"", "Ten", "Twenty", "Thirty", "Forty", "Fifty", "Sixty", _
                                                 "Seventy", "Eighty", "Ninety"}
  Private TenthDigitStringArray() As String = {"Ten", "Eleven", "Tweleve", "Thirteen", "Fourteen", _
                                              "Fifteen", "Sixteen", "Seventeen", "Eighteen", "Nineteen"} 

Background

Hindi numbering system, like other South Asian numbering system, is very alike. The last three digits from right are ready in one way then then higher order digits are read similar to the 10th place digit but with a suffix of higher order digit word.

Example:
12,12,112 = Twelve lakh twelve thousand one hundred twelve
12,00,000 = Twelve lakh
      12,000 =                    twelve thousand
            112 =                                               one hundred twelve

12,12,112 = बारह लाख बारह हजार एक सौ बारह
12,00,000 =
बारह लाख
      12,000 =                       
बारह हजार
            112 =                                                
एक सौ बारह

Code Flow

The entire process is basically array and string manipulation. The primary goal is to find the correct index corresponding to the number and its position and then pulling the corresponding word out of the array shown above.

Below is the main function that converts giving number to Hindi words. Zero is exceptional case so we have to be careful at every step when working with digit zero. The very first thing we do is convert the given number to string and then to an array, by calling NumberToArray for example, 1234 to “1234” then to {1, 2, 3, 4}.

Now the fun begins. We first find out in which place the given digits falls in like, unit, tenth, hundredth, and so on by using SouthAsianCodeArray. The logic behind this array is very simple, explained later in the article.  Once we know the place of the digit we can trisect the case as if it’s in unit place, tenth place and other place. When working with these numbers, we take advantage of both backward (i variable) and forward (<code>j variable) indices.

VB.NET
Private Function HindiStyle() As String
    Dim amountString As String = Amount.ToString

    If Amount = 0 Then Return "शून्य" 'Unique and exceptional case
    If amountString.Length > 15 Then Return "That's too long..."

    Dim amountArray() As Integer = NumberToArray(amountString)

    Dim j As Integer = 0
    Dim digit As Integer = 0
    Dim result As String = ""
    Dim separator As String = ""
    Dim higherDigitHindiString As String = ""
    Dim codeIndex As String = ""


    For i As Integer = amountArray.Length To 1 Step -1
      j = amountArray.Length - i
      digit = amountArray(j)

      codeIndex = SouthAsianCodeArray(i - 1)
      higherDigitHindiString = HigherDigitHindiNumberArray(CInt(codeIndex.Substring(0, 1)) - 1)


      If codeIndex = "1" Then 'Number [1, 9]
        result = result & separator & HundredHindiDigitArray(digit)

      ElseIf codeIndex.Length = 2 And digit <> 0 Then 'Number in tenth place and skip if digit is 0
        Dim suffixDigit As Integer = amountArray(j + 1)
        Dim wholeTenthPlaceDigit As Integer = digit * 10 + suffixDigit

        result = result & separator & HundredHindiDigitArray(wholeTenthPlaceDigit) & " " & _
                                       higherDigitHindiString
        i -= 1

      ElseIf digit <> 0 Then  'Standard Number like 100, 1000, 1000000 and skip if digit is 0
        result = result & separator & HundredHindiDigitArray(digit) & " " & higherDigitHindiString
      End If

      separator = " "
    Next

    Return RemoveSpaces(result)
End Function 

Remove extra spaces:

During the process a space or two get attached in between the words so for the cleanup I use the <span style="font-size: 10pt; line-height: 115%; font-family: 'Courier New'; ">RegEx</span><span style="font-size: 12pt; line-height: 115%; font-family: 'Times New Roman', serif; "> </span>and call the RemoveSpaces function as:
VB.NET
Private Function RemoveSpaces(ByVal word As String) As String
    Dim regEx As New System.Text.RegularExpressions.Regex("  ")
    Return regEx.Replace(word, " ").Trim
End Function 

Number formatting (or grouping):

There is another public function
FormatNumber
which basically calls a private
FormatNumberPerLanguage
in the Converter class. This FormatNumberPerLanguage will format group based on the provided regional name which is “hi-IN” in this case. A simple use of CultureInfo class.
VB.NET
Private Function FormatNumberPerLanguage(ByVal culterInfoName As String)
    Dim ci As New System.Globalization.CultureInfo(culterInfoName)
    ci.NumberFormat.NumberDecimalDigits = 0
    Return Me.Amount.ToString("N", ci)
End Function

Points of Interest  

These arrays that helps to find the place where the numbers falls in are quite important and interesting. For example, in 123456 number, from right, 1 is at 6th position. Now from the <span style="font-size: 10pt; line-height: 115%; font-family: 'Courier New'; ">AsianCodeArray</span> the 6th item is "52" which tells two things:

a) the given number is in tenth position (of some order)
b)  and the higher order is in Lakh's position because of the first letter of 52 is 5 and 4th item (5-1 = 4) in HigherDigitSouthAsianStringArray or HigherDigitHindiNumberArray is Lakh or लाख

This is how I determine the higher order prefixing word!

See Also

This is just an example of using the logic developed in this Convert Numbers to Words page. Using similar logic we can convert to several native languages. If time permits, I soon will post such conversion for other native languages.

License

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


Written By
Engineer
Nepal Nepal
I am not a programer but like to monkey around with the codes. I started with Excel VBA then jumped to VB.NET and spending my time at home with codes. CodeProject helped me a lot to learn and I am always thankful to all the individuals to make this happen.

Grow upwards and onwards!

Comments and Discussions

 
QuestionHow to use Downloaded File Pin
Member 1472013227-Nov-20 21:01
Member 1472013227-Nov-20 21:01 
QuestionHindi fonts not displaying in vb Pin
Member 1489852924-Jul-20 22:38
Member 1489852924-Jul-20 22:38 
QuestionHow can i use these codes in simple way casue i dont have software knowledge Pin
Member 1472013219-Jan-20 2:46
Member 1472013219-Jan-20 2:46 
QuestionInstallation of file Pin
Member 118676912-Aug-16 1:36
Member 118676912-Aug-16 1:36 
AnswerMessage Closed Pin
22-Oct-19 23:43
Member 1412936022-Oct-19 23:43 
AnswerRe: Installation of file Pin
OriginalGriff22-Oct-19 23:44
mveOriginalGriff22-Oct-19 23:44 
Questionhow to use it step by step Pin
Member 1262983911-Jul-16 16:30
Member 1262983911-Jul-16 16:30 
AnswerRe: how to use it step by step Pin
Member 1274607518-Sep-16 2:46
Member 1274607518-Sep-16 2:46 
GeneralRe: how to use it step by step Pin
Member 1274607518-Sep-16 2:49
Member 1274607518-Sep-16 2:49 
Questionhow to use this formula Pin
Member 1225006617-Jan-16 2:15
Member 1225006617-Jan-16 2:15 
Questionexcellent job bro Pin
Member 1208470924-Oct-15 18:06
Member 1208470924-Oct-15 18:06 
QuestionIn article: Numbers to Indian (Hindi) Words Conversion In Unicode. Pin
Member 1134964314-Jan-15 19:27
Member 1134964314-Jan-15 19:27 
Questionhow can i use it Pin
Member 1048648324-Dec-13 21:03
Member 1048648324-Dec-13 21:03 
GeneralMy vote of 5 Pin
Manoj Kumar Choubey30-Jul-13 19:16
professionalManoj Kumar Choubey30-Jul-13 19:16 
QuestionHow to install Pin
PappuS Shukla16-Mar-13 4:04
PappuS Shukla16-Mar-13 4:04 
GeneralMy vote of 3 Pin
navkul30-Aug-12 1:56
navkul30-Aug-12 1:56 
QuestionRe: My vote of 3 Pin
Maciej Los30-Sep-13 6:40
mveMaciej Los30-Sep-13 6:40 
GeneralMy vote of 5 Pin
kburman622-Feb-12 4:13
professionalkburman622-Feb-12 4:13 
GeneralMy vote of 5 Pin
Manoj Kumar Choubey13-Feb-12 1:56
professionalManoj Kumar Choubey13-Feb-12 1:56 
QuestionPerfect. Pin
JAYASH SHARMA10-Feb-12 16:51
JAYASH SHARMA10-Feb-12 16:51 
QuestionNice Article ! Pin
Jαved4-Jan-12 2:18
professionalJαved4-Jan-12 2:18 
GeneralMy vote of 5 Pin
Shahan Ayyub31-Dec-11 6:42
Shahan Ayyub31-Dec-11 6:42 
Question5! : I am Indian Pin
Pranit Kothari30-Dec-11 1:50
Pranit Kothari30-Dec-11 1:50 
AnswerRe: 5! : I am Indian Pin
Musa Biralo1-Jan-12 12:13
Musa Biralo1-Jan-12 12:13 
GeneralMy vote of 5 Pin
LaxmikantYadav29-Dec-11 17:37
LaxmikantYadav29-Dec-11 17:37 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.