Convert numeric to pashto words in C#


Add the following class to your project. 
Create the object of a class and call required function
  • changeNumericToWords()
  • changeCurrencyToWords()



 using System;
using System.Data;
using System.Configuration;
using System.Web;

/*Purpose: Covert number to pashto words
  Created By: Zafar Ali Khan
 */
/// <summary>
/// This class is used to convert digits to words in pashto language (number, currency)
/// </summary>
public class NumberToPokhto
{

    public String changeNumericToWords(double numb)
    {

        String num = numb.ToString();

        return changeToWords(num, false);

    }

    public String changeCurrencyToWords(String numb)
    {

        return changeToWords(numb, true);

    }

    public String changeNumericToWords(String numb)
    {
        if (numb.Trim() == "")
            return "0";
        else if (numb.Trim() == "0")
            return " صفر ";
        else if (numb.Trim() == "100")
            return " سل ";
        else if (numb.Trim() == "1000")
            return " زر ";
        else if (numb.Trim() == "10000")
            return " لس زرا ";
        else if (numb.Trim() == "100000")
            return " سل زرا ";
        else
            return changeToWords(numb, false);

    }

    public String changeCurrencyToWords(double numb)
    {

        return changeToWords(numb.ToString(), true);

    }

    private String changeToWords(String numb, bool isCurrency)
    {

        String val = "", wholeNo = numb, points = "", andStr = "", pointStr = "";

        String endStr = (isCurrency) ? ("صرف") : ("");

        try
        {

            int decimalPlace = numb.IndexOf(".");

            if (decimalPlace > 0)
            {

                wholeNo = numb.Substring(0, decimalPlace);

                points = numb.Substring(decimalPlace + 1);

                if (Convert.ToInt32(points) > 0)
                {

                    andStr = (isCurrency) ? ("او") : ("اعشاریه");// just to separate whole numbers from points/cents

                    endStr = (isCurrency) ? ("پیسی " + endStr) : ("");

                    pointStr = translateCents(points);

                }

            }

            val = String.Format("{0} {1}{2} {3}", translateWholeNumber(wholeNo).Trim(), andStr, pointStr, endStr);

        }

        catch { ;}

        return val;

    }

    private String translateWholeNumber(String number)
    {

        string word = "";

        try
        {

            bool beginsZero = false;//tests for 0XX

            bool isDone = false;//test if already translated

            double dblAmt = (Convert.ToDouble(number));

            //if ((dblAmt > 0) && number.StartsWith("0"))

            if (dblAmt > 0)
            {//test for zero or digit zero in a nuemric

                beginsZero = number.StartsWith("0");

                int numDigits = number.Length;

                int pos = 0;//store digit grouping

                String place = "";//digit grouping name:hundres,thousand,etc...

                switch (numDigits)
                {

                    case 1://ones' range

                        word = ones(number);

                        isDone = true;

                        break;

                    case 2://tens' range

                        word = tens(number);

                        isDone = true;

                        break;

                    case 3://hundreds' range

                        pos = (numDigits % 3) + 1;
                        if (beginsZero)
                        {
                            place = "";
                            break;
                        }
                        if (number.StartsWith("1"))
                            place = " سل ";
                        else
                            place = " سوه ";

                        break;

                    case 4://thousands' range
                        pos = (numDigits % 4) + 1;
                        if (number.StartsWith("1"))
                            place = " زر ";
                        else
                            place = " زرا ";

                        break;
                    case 5:

                    case 6:

                        pos = (numDigits % 4) + 1;

                        place = " زرا ";

                        break;

                    case 7://millions' range

                    case 8:

                    case 9:

                        pos = (numDigits % 7) + 1;
                        if (number.StartsWith("1"))
                            place = " میلون ";
                        else
                            place = " میلونه ";

                        break;

                    case 10://Billions's range

                        pos = (numDigits % 10) + 1;

                        place = " لس میلونه ";

                        break;

                    //add extra case options for anything above Billion...

                    default:

                        isDone = true;

                        break;

                }

                if (!isDone)
                {//if transalation is not done, continue...(Recursion comes in now!!)

                    word = translateWholeNumber(number.Substring(0, pos)) + place + translateWholeNumber(number.Substring(pos));

                    //check for trailing zeros

                    if (beginsZero) word = " او " + word.Trim();

                }

                //ignore digit grouping names

                if (word.Trim().Equals(place.Trim())) word = "";

            }

        }

        catch { ;}

        return word.Trim();

    }

    private String tens(String digit)
    {

        int digt = Convert.ToInt32(digit);

        String name = null;

        switch (digt)
        {

            case 10:

                name = "لس";

                break;

            case 11:

                name = "یوولس";

                break;

            case 12:

                name = "دولس";

                break;

            case 13:

                name = "دیارلس";

                break;

            case 14:

                name = "څوارلس";

                break;

            case 15:

                name = "پنځلس";

                break;

            case 16:

                name = "شپاړس";

                break;

            case 17:

                name = "وولس";

                break;

            case 18:

                name = "اتلس";

                break;

            case 19:

                name = "نونس";

                break;

            case 20:

                name = "شل";

                break;

            case 30:

                name = "دیرش";

                break;

            case 40:

                name = "څلویښت";

                break;

            case 50:

                name = "پنځوس";

                break;

            case 60:

                name = "شپیته";

                break;

            case 70:

                name = "اویا";

                break;

            case 80:

                name = "اتیا";

                break;

            case 90:

                name = "نویی";
                break;
            case 21:
                name = "یوویشت";

                break;
            case 22:
                name = "دواویشت";
                break;
            case 23:
                name = "درویشت";
                break;
            case 24:
                name = "څلرویشت";
                break;
            case 25:
                name = "پنځویشت";
                break;
            case 26:
                name = "شپږویشت";
                break;
            case 27:
                name = "وویشت";
                break;
            case 28:
                name = "اتویشت";
                break;
            case 29:
                name = "نهویشت";
                break;


            default:

                if (digt > 0)
                {

                    name = ones(digit.Substring(1)) + " " + tens(digit.Substring(0, 1) + "0");


                }

                break;

        }

        return name;

    }

    private String ones(String digit)
    {

        int digt = Convert.ToInt32(digit);

        String name = "";

        switch (digt)
        {

            case 1:

                name = "یو";

                break;

            case 2:

                name = "دوه";

                break;

            case 3:

                name = "دری";

                break;

            case 4:

                name = "څلور";

                break;

            case 5:

                name = "پنځه";

                break;

            case 6:

                name = "شپږ";

                break;

            case 7:

                name = "اوهّ";

                break;

            case 8:

                name = "اتهّ";

                break;

            case 9:

                name = "نههّ";

                break;

        }

        return name;

    }

    private String translateCents(String cents)
    {

        String cts = "", digit = "", engOne = "";

        for (int i = 0; i < cents.Length; i++)
        {

            digit = cents[i].ToString();

            if (digit.Equals("0"))
            {

                engOne = "صفر";

            }

            else
            {

                engOne = ones(digit);

            }

            cts += " " + engOne;

        }

        return cts;

    }
    public string IntegerToWords(long inputNum)
    {
        int dig1, dig2, dig3, level = 0, lasttwo, threeDigits;

        string retval = "";
        string x = "";
        string[] ones ={
    "صفر",
    "یو",
    "دوه",
    "دری",
    "څلور",
    "پنځه",
    "شپږ",
    "اوهّ",
    "اتهّ",
    "نههّ",
    "لس",
    "یوولس",
    "دولس",
    "دیارلس",
    "څوارلس",
    "پنځلس",
    "شپاړس",
    "وولس",
    "اتلس",
    "نولس"
  };
        string[] tens ={
    "صفر",
    "لس",
    "شل",
    "دیرش",
    "څلویښت",
    "پنځوس",
    "شپیتهّ",
    "اویا",
    "اتیا",
    "نویی"
  };
        string[] thou ={
    "",
    "زر",
    "میلون",
    "لس میلونه",
    "سل میلونه",
    "quadrillion",
    "quintillion"
  };

        bool isNegative = false;
        if (inputNum < 0)
        {
            isNegative = true;
            inputNum *= -1;
        }

        if (inputNum == 0)
            return ("zero");

        string s = inputNum.ToString();

        while (s.Length > 0)
        {
            // Get the three rightmost characters
            x = (s.Length < 3) ? s : s.Substring(s.Length - 3, 3);

            // 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
                    retval = tens[dig2] + " " + ones[dig3] + " " + retval;
            }

            // if a hundreds part is there, translate it
            if (dig1 > 0)
                retval = ones[dig1] + " سل " + retval;

            s = (s.Length - 3) > 0 ? s.Substring(0, s.Length - 3) : "";
            level++;
        }

        while (retval.IndexOf("  ") > 0)
            retval = retval.Replace("  ", " ");

        retval = retval.Trim();

        if (isNegative)
            retval = " منفی " + retval;

        return (retval);
    }


}

Using the class

 private void button1_Click(object sender, EventArgs e)
        {
            NumberToPokhto ntw = new NumberToPokhto();
            double digit=double.Parse(textBox1.Text);
            lblResult.Text = ntw.changeNumericToWords(digit);
        }

Post a Comment