Debashish Mahonta

Debashish Mahonta

  • 1.5k
  • 124
  • 2k

Converting VB code to C#

Mar 29 2021 6:18 AM
I want to convert vb code to c# Here is the code.
  1. Public Class clsNum2Word  
  2.   
  3.     Public Function Num2WordConverter(ByVal Number As String)  
  4.   
  5.         Dim Words As String  
  6.         Dim Length As Integer  
  7.         Dim Whole As Decimal  
  8.         Dim Fraction As Decimal  
  9.   
  10.         Whole = Math.Floor(Convert.ToDecimal(Number))  
  11.   
  12.         Fraction = (Convert.ToDecimal(Number) - Whole).ToString + "000"  
  13.   
  14.         Length = Whole.ToString.Length  
  15.   
  16.         If Fraction.ToString.Length >= 4 Then 'coz if the fraction part is just 0 it will generate error in substring  
  17.             Fraction = Convert.ToInt32(Fraction.ToString.Substring(2, 2))  
  18.         End If  
  19.   
  20.         If Length = 9 Then  'For 10 to 99 Crores  
  21.   
  22.             Words = MakeWord(Convert.ToInt32(Whole.ToString.Substring(0, 2))) + " Crore(s)"  
  23.   
  24.             If Convert.ToInt32(Whole.ToString.Substring(2, 2)) <> 0 Then  
  25.                 Words += " " + MakeWord(Convert.ToInt32(Whole.ToString.Substring(2, 2))) + " Lac(s)"  
  26.             End If  
  27.   
  28.   
  29.             If Convert.ToInt32(Whole.ToString.Substring(4, 2)) <> 0 Then  
  30.                 Words += " " + MakeWord(Convert.ToInt32(Whole.ToString.Substring(4, 2))) + " Thousand"  
  31.             End If  
  32.   
  33.   
  34.             If Convert.ToInt32(Whole.ToString.Substring(6, 1)) <> 0 Then  
  35.                 Words += " " + MakeWord(Convert.ToInt32(Whole.ToString.Substring(6, 1))) + " Hundred"  
  36.             End If  
  37.   
  38.             If Convert.ToInt32(Whole.ToString.Substring(7, 2)) <> 0 Then  
  39.                 Words += " " + MakeWord(Convert.ToInt32(Whole.ToString.Substring(7, 2)))  
  40.             End If  
  41.   
  42.             Words += " Rupee(s) And " + MakeWord(Fraction) + " Paise Only."  
  43.   
  44.   
  45.   
  46.         ElseIf Length = 8 Then  
  47.   
  48.             Words = MakeWord(Convert.ToInt32(Whole.ToString.Substring(0, 1))) + " Crore(s)"  
  49.   
  50.             If Convert.ToInt32(Whole.ToString.Substring(1, 2)) <> 0 Then  
  51.                 Words += " " + MakeWord(Convert.ToInt32(Whole.ToString.Substring(1, 2))) + " Lac(s)"  
  52.             End If  
  53.   
  54.   
  55.             If Convert.ToInt32(Whole.ToString.Substring(3, 2)) <> 0 Then  
  56.                 Words += " " + MakeWord(Convert.ToInt32(Whole.ToString.Substring(3, 2))) + " Thousand"  
  57.             End If  
  58.   
  59.   
  60.             If Convert.ToInt32(Whole.ToString.Substring(5, 1)) <> 0 Then  
  61.                 Words += " " + MakeWord(Convert.ToInt32(Whole.ToString.Substring(5, 1))) + " Hundred"  
  62.             End If  
  63.   
  64.             If Convert.ToInt32(Whole.ToString.Substring(6, 2)) <> 0 Then  
  65.                 Words += " " + MakeWord(Convert.ToInt32(Whole.ToString.Substring(6, 2)))  
  66.             End If  
  67.   
  68.             Words += " Rupee(s) And " + MakeWord(Fraction) + " Paise Only."  
  69.   
  70.   
  71.         ElseIf Length = 7 Then  
  72.   
  73.             Words = MakeWord(Convert.ToInt32(Whole.ToString.Substring(0, 2))) + " Lac(s)"  
  74.   
  75.             If Convert.ToInt32(Whole.ToString.Substring(2, 2)) <> 0 Then  
  76.                 Words += " " + MakeWord(Convert.ToInt32(Whole.ToString.Substring(2, 2))) + " Thousand"  
  77.             End If  
  78.   
  79.   
  80.             If Convert.ToInt32(Whole.ToString.Substring(4, 1)) <> 0 Then  
  81.                 Words += " " + MakeWord(Convert.ToInt32(Whole.ToString.Substring(4, 1))) + " Hundred"  
  82.             End If  
  83.   
  84.             If Convert.ToInt32(Whole.ToString.Substring(5, 2)) <> 0 Then  
  85.                 Words += " " + MakeWord(Convert.ToInt32(Whole.ToString.Substring(5, 2)))  
  86.             End If  
  87.   
  88.             Words += " Rupee(s) And " + MakeWord(Fraction) + " Paise Only."  
  89.   
  90.         ElseIf Length = 6 Then  
  91.   
  92.             Words = MakeWord(Convert.ToInt32(Whole.ToString.Substring(0, 1))) + " lac(s)"  
  93.   
  94.             If Convert.ToInt32(Whole.ToString.Substring(1, 2)) <> 0 Then  
  95.                 Words += " " + MakeWord(Convert.ToInt32(Whole.ToString.Substring(1, 2))) + " Thousand"  
  96.             End If  
  97.   
  98.   
  99.             If Convert.ToInt32(Whole.ToString.Substring(3, 1)) <> 0 Then  
  100.                 Words += " " + MakeWord(Convert.ToInt32(Whole.ToString.Substring(3, 1))) + " Hundred"  
  101.             End If  
  102.   
  103.             If Convert.ToInt32(Whole.ToString.Substring(4, 2)) <> 0 Then  
  104.                 Words += " " + MakeWord(Convert.ToInt32(Whole.ToString.Substring(4, 2)))  
  105.             End If  
  106.   
  107.             Words += " Rupee(s) And " + MakeWord(Fraction) + " Paise Only."  
  108.   
  109.         ElseIf Length = 5 Then  
  110.   
  111.             Words = MakeWord(Convert.ToInt32(Whole.ToString.Substring(0, 2))) + " Thousand"  
  112.   
  113.             If Convert.ToInt32(Whole.ToString.Substring(2, 1)) <> 0 Then  
  114.                 Words += " " + MakeWord(Convert.ToInt32(Whole.ToString.Substring(2, 1))) + " Hundred"  
  115.             End If  
  116.   
  117.             If Convert.ToInt32(Whole.ToString.Substring(3, 2)) <> 0 Then  
  118.                 Words += " " + MakeWord(Convert.ToInt32(Whole.ToString.Substring(3, 2)))  
  119.             End If  
  120.   
  121.             Words += " Rupee(s) And " + MakeWord(Fraction) + " Paise Only."  
  122.   
  123.         ElseIf Length = 4 Then  
  124.   
  125.             Words = MakeWord(Convert.ToInt32(Whole.ToString.Substring(0, 1))) + " Thousand"  
  126.   
  127.             If Convert.ToInt32(Whole.ToString.Substring(1, 1)) <> 0 Then  
  128.                 Words += " " + MakeWord(Convert.ToInt32(Whole.ToString.Substring(1, 1))) + " Hundred"  
  129.             End If  
  130.   
  131.             If Convert.ToInt32(Whole.ToString.Substring(2, 2)) <> 0 Then  
  132.                 Words += " " + MakeWord(Convert.ToInt32(Whole.ToString.Substring(2, 2)))  
  133.             End If  
  134.   
  135.             Words += " Rupee(s) And " + MakeWord(Fraction) + " Paise Only."  
  136.   
  137.         ElseIf Length = 3 Then  
  138.   
  139.             Words = MakeWord(Convert.ToInt32(Whole.ToString.Substring(0, 1))) + " Hundred"  
  140.   
  141.             If Convert.ToInt32(Whole.ToString.Substring(1, 2)) <> 0 Then  
  142.                 Words += " " + MakeWord(Convert.ToInt32(Whole.ToString.Substring(1, 2)))  
  143.             End If  
  144.   
  145.             Words += " Rupee(s) And " + MakeWord(Fraction) + " Paise Only."  
  146.   
  147.         ElseIf Length <= 2 Then  
  148.             Words = MakeWord(Whole) + " Rupee(s) And " + MakeWord(Fraction) + " Paise Only."  
  149.         Else  
  150.             Words = "Range Exceeded."  
  151.         End If  
  152.   
  153.   
  154.         Return Words  
  155.   
  156.     End Function  
  157.   
  158.   
  159.       
  160.     Private Function MakeWord(ByVal Number As Integer)  
  161.   
  162.         Select Case Number  
  163.             Case 0  
  164.                 Return "Zero"  
  165.             Case 1  
  166.                 Return "One"  
  167.             Case 2  
  168.                 Return "Two"  
  169.             Case 3  
  170.                 Return "Three"  
  171.             Case 4  
  172.                 Return "Four"  
  173.             Case 5  
  174.                 Return "Five"  
  175.             Case 6  
  176.                 Return "Six"  
  177.             Case 7  
  178.                 Return "Seven"  
  179.             Case 8  
  180.                 Return "Eight"  
  181.             Case 9  
  182.                 Return "Nine"  
  183.             Case 10  
  184.                 Return "Ten"  
  185.             Case 11  
  186.                 Return "Eleven"  
  187.             Case 12  
  188.                 Return "Tweleve"  
  189.             Case 13  
  190.                 Return "Thirteen"  
  191.             Case 14  
  192.                 Return "Fourteen"  
  193.             Case 15  
  194.                 Return "Fifteen"  
  195.             Case 16  
  196.                 Return "Sixteen"  
  197.             Case 17  
  198.                 Return "Seventeen"  
  199.             Case 18  
  200.                 Return "Eighteen"  
  201.             Case 19  
  202.                 Return "Nineteen"  
  203.             Case 20  
  204.                 Return "Twenty"  
  205.             Case 21  
  206.                 Return "Twenty One"  
  207.             Case 22  
  208.                 Return "Twenty Two"  
  209.             Case 23  
  210.                 Return "Twenty Three"  
  211.             Case 24  
  212.                 Return "Twenty Four"  
  213.             Case 25  
  214.                 Return "Twenty Five"  
  215.             Case 26  
  216.                 Return "Twenty Six"  
  217.             Case 27  
  218.                 Return "Twenty Seven"  
  219.             Case 28  
  220.                 Return "Twenty Eight"  
  221.             Case 29  
  222.                 Return "Twenty Nine"  
  223.             Case 30  
  224.                 Return "Thirty"  
  225.             Case 31  
  226.                 Return "Thirty One"  
  227.             Case 32  
  228.                 Return "Thirty Two"  
  229.             Case 33  
  230.                 Return "Thirty Three"  
  231.             Case 34  
  232.                 Return "Thirty Four"  
  233.             Case 35  
  234.                 Return "Thirty Five"  
  235.             Case 36  
  236.                 Return "Thirty Six"  
  237.             Case 37  
  238.                 Return "Thirty Seven"  
  239.             Case 38  
  240.                 Return "Thirty Eight"  
  241.             Case 39  
  242.                 Return "Thirty Nine"  
  243.             Case 40  
  244.                 Return "Forty"  
  245.             Case 41  
  246.                 Return "Forty One"  
  247.             Case 42  
  248.                 Return "Forty Two"  
  249.             Case 43  
  250.                 Return "Forty Three"  
  251.             Case 44  
  252.                 Return "Forty Four"  
  253.             Case 45  
  254.                 Return "Forty Five"  
  255.             Case 46  
  256.                 Return "Forty Six"  
  257.             Case 47  
  258.                 Return "Forty Seven"  
  259.             Case 48  
  260.                 Return "Forty Eight"  
  261.             Case 49  
  262.                 Return "Forty Nine"  
  263.             Case 50  
  264.                 Return "Fifty"  
  265.             Case 51  
  266.                 Return "Fifty One"  
  267.             Case 52  
  268.                 Return "Fifty Two"  
  269.             Case 53  
  270.                 Return "Fifty Three"  
  271.             Case 54  
  272.                 Return "Fifty Four"  
  273.             Case 55  
  274.                 Return "Fifty Five"  
  275.             Case 56  
  276.                 Return "Fifty Six"  
  277.             Case 57  
  278.                 Return "Fifty Seven"  
  279.             Case 58  
  280.                 Return "Fifty Eight"  
  281.             Case 59  
  282.                 Return "Fifty Nine"  
  283.             Case 60  
  284.                 Return "Sixty"  
  285.             Case 61  
  286.                 Return "Sixty One"  
  287.             Case 62  
  288.                 Return "Sixty Two"  
  289.             Case 63  
  290.                 Return "Sixty Three"  
  291.             Case 64  
  292.                 Return "Sixty Four"  
  293.             Case 65  
  294.                 Return "Sixty Five"  
  295.             Case 66  
  296.                 Return "Sixty Six"  
  297.             Case 67  
  298.                 Return "Sixty Seven"  
  299.             Case 68  
  300.                 Return "Sixty Eight"  
  301.             Case 69  
  302.                 Return "Sixty Nine"  
  303.             Case 70  
  304.                 Return "Seventy"  
  305.             Case 71  
  306.                 Return "Seventy One"  
  307.             Case 72  
  308.                 Return "Seventy Two"  
  309.             Case 73  
  310.                 Return "Seventy Three"  
  311.             Case 74  
  312.                 Return "Seventy Four"  
  313.             Case 75  
  314.                 Return "Seventy Five"  
  315.             Case 76  
  316.                 Return "Seventy Six"  
  317.             Case 77  
  318.                 Return "Seventy Seven"  
  319.             Case 78  
  320.                 Return "Seventy Eight"  
  321.             Case 79  
  322.                 Return "Seventy Nine"  
  323.             Case 80  
  324.                 Return "Eighty"  
  325.             Case 81  
  326.                 Return "Eighty One"  
  327.             Case 82  
  328.                 Return "Eighty Two"  
  329.             Case 83  
  330.                 Return "Eighty Three"  
  331.             Case 84  
  332.                 Return "Eighty Four"  
  333.             Case 85  
  334.                 Return "Eighty Five"  
  335.             Case 86  
  336.                 Return "Eighty Six"  
  337.             Case 87  
  338.                 Return "Eighty Seven"  
  339.             Case 88  
  340.                 Return "Eighty Eight"  
  341.             Case 89  
  342.                 Return "Eighty Nine"  
  343.             Case 90  
  344.                 Return "Ninety"  
  345.             Case 91  
  346.                 Return "Ninety One"  
  347.             Case 92  
  348.                 Return "Ninety Two"  
  349.             Case 93  
  350.                 Return "Ninety Three"  
  351.             Case 94  
  352.                 Return "Ninety Four"  
  353.             Case 95  
  354.                 Return "Ninety Five"  
  355.             Case 96  
  356.                 Return "Ninety Six"  
  357.             Case 97  
  358.                 Return "Ninety Seven"  
  359.             Case 98  
  360.                 Return "Ninety Eight"  
  361.             Case 99  
  362.                 Return "Ninety Nine"  
  363.             Case Else  
  364.                 Return "Error"  
  365.         End Select  
  366.     End Function  
  367.   
  368. End Class 
 Please anyone help me to convert the code for c#

Answers (4)