Numeric to Word Format (Indian Currency) Using ASP.NET


In this article, I have written c# code to get the Currency Value in Indian format. Use the method to convert numeric to word format of Indian currency.

Numeric to Word Format (Indian Currency) Using ASP.NET

Below method is created using ASP.NET and can be used to convert numeric to word format of Indian currency.


public string IntegerToWords(long inputNum)

{

int dig1, dig2, dig3, level = 0, lasttwo, threeDigits;

string retval = "";

string x = "";

string[] ones ={

};

string[] tens ={

};

string[] thou ={

};

bool isNegative = false;

if (inputNum < 0)

{

isNegative = true;

inputNum *= -1;

}

if (inputNum == 0)

return ("Zero");



string s = inputNum.ToString();

int i = 3;

while (s.Length > 0)

{

// Get the three rightmost characters

x = (s.Length < i) ? s : s.Substring(s.Length - i, i);


// Separate the three digits

threeDigits = int.Parse(x);

lasttwo = threeDigits % 100;

dig1 = threeDigits / 100;

dig2 = lasttwo / 10;

dig3 = (threeDigits % 10);


// append a "thousand" where appropriate

if (level > 0 && dig1 + dig2 + dig3 > 0)

{

retval = thou[level] + " " + retval;

retval = retval.Trim();

}

// check that the last two digits is not a zero

if (lasttwo > 0)

{

if (lasttwo < 20) // if less than 20, use "ones" only

retval = ones[lasttwo] + " " + retval;

else // otherwise, use both "tens" and "ones" array

if (ones[dig3].ToString() != "Zero")

{

retval = tens[dig2] + " " + ones[dig3] + " " + retval;

}

else

{

retval = tens[dig2] + " " + retval;

}

}

// if a hundreds part is there, translate it

if (dig1 > 0 && level == 0)

retval = ones[dig1] + " Hundred " + " and " + retval;

s = (s.Length - i) > 0 ? s.Substring(0, s.Length - i) : "";

level++;

i = 2;

}



while (retval.IndexOf(" ") > 0)

retval = retval.Replace(" ", " ");

retval = retval.Trim()+" only";


if (isNegative)

retval = "negative " + retval;


return (retval);

}


Comments

No responses found. Be the first to comment...


  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name:
    Email: