Answered by:
Display Numerical in word format

Question
-
User810354248 posted
In my aspx+vb web i have a numerical label in which amount is displayed.
Value is 174,798 and its converted to words as under : It should be One lakhs Seventy Four Thousand Seven Hundred and Ninty Eight
One Hundred Seventy-Four Thousand Seven Hundred Ninety-Eight
Code is as under
Public Function AmountInWords(ByVal nAmount As String, Optional ByVal wAmount _ As String = vbNullString, Optional ByVal nSet As Object = Nothing) As String 'Let's make sure entered value is numeric If Not IsNumeric(nAmount) Then Return "Please enter numeric values only." Dim tempDecValue As String = String.Empty : If InStr(nAmount, ".") Then _ tempDecValue = nAmount.Substring(nAmount.IndexOf(".")) nAmount = Replace(nAmount, tempDecValue, String.Empty) Try Dim intAmount As Long = nAmount If intAmount > 0 Then nSet = IIf((intAmount.ToString.Trim.Length / 3) _ > (CLng(intAmount.ToString.Trim.Length / 3)), _ CLng(intAmount.ToString.Trim.Length / 3) + 1, _ CLng(intAmount.ToString.Trim.Length / 3)) Dim eAmount As Long = Microsoft.VisualBasic.Left(intAmount.ToString.Trim, _ (intAmount.ToString.Trim.Length - ((nSet - 1) * 3))) Dim multiplier As Long = 10 ^ (((nSet - 1) * 3)) Dim Ones() As String = _ {"", "One", "Two", "Three", _ "Four", "Five", _ "Six", "Seven", "Eight", "Nine"} Dim Teens() As String = {"", _ "Eleven", "Twelve", "Thirteen", _ "Fourteen", "Fifteen", _ "Sixteen", "Seventeen", "Eighteen", "Nineteen"} Dim Tens() As String = {"", "Ten", _ "Twenty", "Thirty", _ "Forty", "Fifty", "Sixty", _ "Seventy", "Eighty", "Ninety"} Dim HMBT() As String = {"", "", _ "Thousand", "Million", _ "Billion", "Trillion", _ "Quadrillion", "Quintillion"} intAmount = eAmount Dim nHundred As Integer = intAmount \ 100 : intAmount = intAmount Mod 100 Dim nTen As Integer = intAmount \ 10 : intAmount = intAmount Mod 10 Dim nOne As Integer = intAmount \ 1 If nHundred > 0 Then wAmount = wAmount & _ Ones(nHundred) & " Hundred " 'This is for hundreds If nTen > 0 Then 'This is for tens and teens If nTen = 1 And nOne > 0 Then 'This is for teens wAmount = wAmount & Teens(nOne) & " " Else 'This is for tens, 10 to 90 wAmount = wAmount & Tens(nTen) & IIf(nOne > 0, "-", " ") If nOne > 0 Then wAmount = wAmount & Ones(nOne) & " " End If Else 'This is for ones, 1 to 9 If nOne > 0 Then wAmount = wAmount & Ones(nOne) & " " End If wAmount = wAmount & HMBT(nSet) & " " wAmount = AmountInWords(CStr(CLng(nAmount) - _ (eAmount * multiplier)).Trim & tempDecValue, wAmount, nSet - 1) Else If Val(nAmount) = 0 Then nAmount = nAmount & _ tempDecValue : tempDecValue = String.Empty If (Math.Round(Val(nAmount), 2) * 100) > 0 Then wAmount = _ Trim(AmountInWords(CStr(Math.Round(Val(nAmount), 2) * 100), _ wAmount.Trim & "", 1)) & "" End If Catch ex As Exception Return "Unable to Convert the Value" End Try 'Trap null values If IsNothing(wAmount) = True Then wAmount = String.Empty Else wAmount = _ IIf(InStr(wAmount.Trim.ToLower, " "), _ wAmount.Trim, wAmount.Trim & " ") 'Display the result Return wAmount End Function
gttl.Text = String.Format("{0:N0}", dta2.Tables("PO_Items").Rows(0).Item(0))
wordstxt.Text = AmountInWords(gttl.Text)
Thursday, January 21, 2021 10:25 AM
Answers
-
User-939850651 posted
Hi Baiju EP,
In the code you provided, the amount is divided into thousands, and the lakhs you mentioned do not exist. If you need to implement this requirement, you could refer to the following code:
Private Shared Function ConvertDecimals(ByVal number As String) As String Dim cd As String = "", digit As String = "", engOne As String = "" For i As Integer = 0 To number.Length - 1 digit = number(i).ToString() If digit.Equals("0") Then engOne = "Zero" Else engOne = ones(digit) End If cd += " " & engOne Next Return cd End Function Private Shared Function ones(ByVal Number As String) As String Dim _Number As Integer = Int32.Parse(Number) Dim name As String = "" Select Case _Number Case 1 name = "One" Case 2 name = "Two" Case 3 name = "Three" Case 4 name = "Four" Case 5 name = "Five" Case 6 name = "Six" Case 7 name = "Seven" Case 8 name = "Eight" Case 9 name = "Nine" End Select Return name End Function Private Shared Function tens(ByVal Number As String) As String Dim _Number As Integer = Int32.Parse(Number) Dim name As String = Nothing Select Case _Number Case 10 name = "Ten" Case 11 name = "Eleven" Case 12 name = "Twelve" Case 13 name = "Thirteen" Case 14 name = "Fourteen" Case 15 name = "Fifteen" Case 16 name = "Sixteen" Case 17 name = "Seventeen" Case 18 name = "Eighteen" Case 19 name = "Nineteen" Case 20 name = "Twenty" Case 30 name = "Thirty" Case 40 name = "Fourty" Case 50 name = "Fifty" Case 60 name = "Sixty" Case 70 name = "Seventy" Case 80 name = "Eighty" Case 90 name = "Ninety" Case Else If _Number > 0 Then name = tens(Number.Substring(0, 1) & "0") & "-" + ones(Number.Substring(1)) End If End Select Return name End Function Private Shared Function AmountInWords(ByVal Number As String) As String Number = Number.Replace(",", "") Dim word As String = "" Try Dim beginsZero As Boolean = False Dim isDone As Boolean = False Dim dblAmt As Double = Double.Parse(Number) If dblAmt > 0 Then beginsZero = Number.StartsWith("0") Dim numDigits As Integer = Number.Length Dim pos As Integer = 0 Dim place As String = "" Select Case numDigits Case 1 word = ones(Number) isDone = True Case 2 word = tens(Number) isDone = True Case 3 pos = (numDigits Mod 3) + 1 place = " Hundred " Case 4, 5 pos = (numDigits Mod 4) + 1 place = " Thousand " Case 6 pos = (numDigits Mod 6) + 1 place = " lakhs " Case 7, 8, 9 pos = (numDigits Mod 7) + 1 place = " Million " Case 10, 11, 12 pos = (numDigits Mod 10) + 1 place = " Billion " Case Else isDone = True End Select If Not isDone Then If Number.Substring(0, pos) <> "0" AndAlso Number.Substring(pos) <> "0" Then Try word = AmountInWords(Number.Substring(0, pos)) + place + AmountInWords(Number.Substring(pos)) Catch End Try Else word = AmountInWords(Number.Substring(0, pos)) + AmountInWords(Number.Substring(pos)) End If End If If word.Trim().Equals(place.Trim()) Then word = "" End If Catch End Try Return word.Trim() End Function Private Shared Function ConvertToWords(ByVal numb As String) As String Dim val As String = "", wholeNo As String = numb, points As String = "", andStr As String = "", pointStr As String = "" Dim endStr As String = "Only" Try Dim decimalPlace As Integer = numb.IndexOf(".") If decimalPlace > 0 Then wholeNo = numb.Substring(0, decimalPlace) points = numb.Substring(decimalPlace + 1) If Int32.Parse(points) > 0 Then andStr = "and" endStr = "Paisa " & endStr pointStr = ConvertDecimals(points) End If End If val = String.Format("{0} {1}{2} {3}", AmountInWords(wholeNo).Trim(), andStr, pointStr, endStr) Catch End Try Return val End Function Protected Sub convert_Click(sender As Object, e As EventArgs) result.Text = AmountInWords(gttl.Text) End Sub
The code includes the conversion of integers and decimals, you could also refer to this article.
Convert Numeric Value Into Words (Currency) In C#
Result:
Best regards,
Xudong Peng
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Friday, January 22, 2021 9:29 AM
All replies
-
User-939850651 posted
Hi Baiju EP,
In the code you provided, the amount is divided into thousands, and the lakhs you mentioned do not exist. If you need to implement this requirement, you could refer to the following code:
Private Shared Function ConvertDecimals(ByVal number As String) As String Dim cd As String = "", digit As String = "", engOne As String = "" For i As Integer = 0 To number.Length - 1 digit = number(i).ToString() If digit.Equals("0") Then engOne = "Zero" Else engOne = ones(digit) End If cd += " " & engOne Next Return cd End Function Private Shared Function ones(ByVal Number As String) As String Dim _Number As Integer = Int32.Parse(Number) Dim name As String = "" Select Case _Number Case 1 name = "One" Case 2 name = "Two" Case 3 name = "Three" Case 4 name = "Four" Case 5 name = "Five" Case 6 name = "Six" Case 7 name = "Seven" Case 8 name = "Eight" Case 9 name = "Nine" End Select Return name End Function Private Shared Function tens(ByVal Number As String) As String Dim _Number As Integer = Int32.Parse(Number) Dim name As String = Nothing Select Case _Number Case 10 name = "Ten" Case 11 name = "Eleven" Case 12 name = "Twelve" Case 13 name = "Thirteen" Case 14 name = "Fourteen" Case 15 name = "Fifteen" Case 16 name = "Sixteen" Case 17 name = "Seventeen" Case 18 name = "Eighteen" Case 19 name = "Nineteen" Case 20 name = "Twenty" Case 30 name = "Thirty" Case 40 name = "Fourty" Case 50 name = "Fifty" Case 60 name = "Sixty" Case 70 name = "Seventy" Case 80 name = "Eighty" Case 90 name = "Ninety" Case Else If _Number > 0 Then name = tens(Number.Substring(0, 1) & "0") & "-" + ones(Number.Substring(1)) End If End Select Return name End Function Private Shared Function AmountInWords(ByVal Number As String) As String Number = Number.Replace(",", "") Dim word As String = "" Try Dim beginsZero As Boolean = False Dim isDone As Boolean = False Dim dblAmt As Double = Double.Parse(Number) If dblAmt > 0 Then beginsZero = Number.StartsWith("0") Dim numDigits As Integer = Number.Length Dim pos As Integer = 0 Dim place As String = "" Select Case numDigits Case 1 word = ones(Number) isDone = True Case 2 word = tens(Number) isDone = True Case 3 pos = (numDigits Mod 3) + 1 place = " Hundred " Case 4, 5 pos = (numDigits Mod 4) + 1 place = " Thousand " Case 6 pos = (numDigits Mod 6) + 1 place = " lakhs " Case 7, 8, 9 pos = (numDigits Mod 7) + 1 place = " Million " Case 10, 11, 12 pos = (numDigits Mod 10) + 1 place = " Billion " Case Else isDone = True End Select If Not isDone Then If Number.Substring(0, pos) <> "0" AndAlso Number.Substring(pos) <> "0" Then Try word = AmountInWords(Number.Substring(0, pos)) + place + AmountInWords(Number.Substring(pos)) Catch End Try Else word = AmountInWords(Number.Substring(0, pos)) + AmountInWords(Number.Substring(pos)) End If End If If word.Trim().Equals(place.Trim()) Then word = "" End If Catch End Try Return word.Trim() End Function Private Shared Function ConvertToWords(ByVal numb As String) As String Dim val As String = "", wholeNo As String = numb, points As String = "", andStr As String = "", pointStr As String = "" Dim endStr As String = "Only" Try Dim decimalPlace As Integer = numb.IndexOf(".") If decimalPlace > 0 Then wholeNo = numb.Substring(0, decimalPlace) points = numb.Substring(decimalPlace + 1) If Int32.Parse(points) > 0 Then andStr = "and" endStr = "Paisa " & endStr pointStr = ConvertDecimals(points) End If End If val = String.Format("{0} {1}{2} {3}", AmountInWords(wholeNo).Trim(), andStr, pointStr, endStr) Catch End Try Return val End Function Protected Sub convert_Click(sender As Object, e As EventArgs) result.Text = AmountInWords(gttl.Text) End Sub
The code includes the conversion of integers and decimals, you could also refer to this article.
Convert Numeric Value Into Words (Currency) In C#
Result:
Best regards,
Xudong Peng
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Friday, January 22, 2021 9:29 AM -
User-1716253493 posted
Maybe there are many js you can use
Sunday, January 24, 2021 1:18 PM