Welcome to Dream.In.Code
Getting C# Help is Easy!

Join 136,522 C# Programmers for FREE! Get instant access to thousands of C# experts, tutorials, code snippets, and more! There are 1,819 people online right now. Registration is fast and FREE... Join Now!




decimal to words in c# windows form

 
Reply to this topicStart new topic

decimal to words in c# windows form, Please help, I'm getting an error

sureshvenkataraman
13 Aug, 2008 - 10:08 PM
Post #1

New D.I.C Head
*

Joined: 13 Aug, 2008
Posts: 2

tongue.gif hi i want number with decimal to words in c# windows form
but error came in (index outside of the arry)
could u help any one

CODE

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Collections;


namespace test
{
    public partial class Form1 : Form
    {

        string Number;
        string deciml;
        string _number;
        string _deciml;
        string[] US = new string[1003];
        string[] SNu = new string[20];
        string[] SNt = new string[10];
        public Form1()
        {
            InitializeComponent();
        }
        
        public class NullReferenceException : SystemException
        {
        }
        public sealed class IndexOutOfRangeException : SystemException
        { }

        private void button1_Click(object sender, EventArgs e)
        {
            string currency = "";
            string _currency = " Paise Only";
            if (Convert.ToDouble(textBox1.Text) == 0)
            {
                label1.Text = "Null Value";
                return;
            }
            if (Convert.ToDouble(textBox1.Text) < 0)
            {
                label1 .Text ="Invalid Value";
                return;
            }

            string[] no = textBox1.Text.Split('.');
            if ((no[0] != null) && (no[1] != "00"))   //errorr came here[email=vsuresh1984@gmail.com]vsuresh1984@gmail.com[/email]
            {
                Number = no[0];
                deciml = no[1];
                _number = (NameOfNumber(Number));
                _deciml = (NameOfNumber(deciml));
                label1.Text = currency + _number.Trim() + " Rupees and " + _deciml.Trim() + _currency;
            }
                if ((no[0] != null) && (no[1] == "00"))
                {
                    Number = no[0];
                    _number = (NameOfNumber(Number));
                    label1.Text = currency + _number + " Rupees Only";
                }
            
            if ((Convert.ToDouble(no[0]) == 0) && (no[1] != null))
            {
                deciml = no[1];
                _deciml = (NameOfNumber(deciml));
                label1.Text = _deciml + _currency;
            }
        }
      
        public string NameOfNumber(string Number)
        {
            string GroupName = "";
            string OutPut = "";

            if ((Number.Length % 3) != 0)
            {
                Number = Number.PadLeft((Number.Length + (3 - (Number.Length % 3))), '0');
            }
            string[] Array = new string[Number.Length / 3];
            Int16 Element = -1;
            Int32 DisplayCount = -1;
            bool LimitGroupsShowAll = false;
            int LimitGroups = 0;
            bool GroupToWords = true;
            for (Int16 Count = 0; Count <= Number.Length - 3; Count += 3)
            {
                Element += 1;
                Array[Element] = Number.Substring(Count, 3);

            }
            if (LimitGroups == 0)
            {
                LimitGroupsShowAll = true;
            }
            for (Int16 Count = 0; (Count <= ((Number.Length / 3) - 1)); Count++)
            {
                DisplayCount++;
                if (((DisplayCount < LimitGroups) || LimitGroupsShowAll))
                {
                    if (Array[Count] == "000") continue;
                    {
                        GroupName = US[((Number.Length / 3) - 1) - Count + 1];
                    }


                    if ((GroupToWords == true))
                    {
                        OutPut += Group(Array[Count]).TrimEnd(' ') + " " + GroupName + " ";

                    }
                    else
                    {
                        OutPut += Array[Count].TrimStart('0') + " " + GroupName;

                    }
                }

            }
            Array = null;
            return OutPut;

        }


        private string Group(string Argument)
        {
            string Hyphen = "";
            string OutPut = "";
            Int16 d1 = Convert.ToInt16(Argument.Substring(0, 1));
            Int16 d2 = Convert.ToInt16(Argument.Substring(1, 1));
            Int16 d3 = Convert.ToInt16(Argument.Substring(2, 1));
            if ((d1 >= 1))
            {
                OutPut += SNu[d1] + " hundred ";
            }
            if ((double.Parse(Argument.Substring(1, 2)) < 20))
            {
                OutPut += SNu[Convert.ToInt16(Argument.Substring(1, 2))];
            }
            if ((double.Parse(Argument.Substring(1, 2)) >= 20))
            {
                if (Convert.ToInt16(Argument.Substring(2, 1)) == 0)
                {
                    Hyphen += " ";
                }
                else
                {
                    Hyphen += " ";
                }
                OutPut += SNt[d2] + Hyphen + SNu[d3];
            }
            return OutPut;
        }

        private void Initialize()
        {

            SNu[0] = "";
            SNu[1] = "One";
            SNu[2] = "Two";
            SNu[3] = "Three";
            SNu[4] = "Four";
            SNu[5] = "Five";
            SNu[6] = "Six";
            SNu[7] = "Seven";
            SNu[8] = "Eight";
            SNu[9] = "Nine";
            SNu[10] = "Ten";
            SNu[11] = "Eleven";
            SNu[12] = "Twelve";
            SNu[13] = "Thirteen";
            SNu[14] = "Fourteen";
            SNu[15] = "Fifteen";
            SNu[16] = "Sixteen";
            SNu[17] = "Seventeen";
            SNu[18] = "Eighteen";
            SNu[19] = "Nineteen";
            SNt[2] = "Twenty";
            SNt[3] = "Thirty";
            SNt[4] = "Forty";
            SNt[5] = "Fifty";
            SNt[6] = "Sixty";
            SNt[7] = "Seventy";
            SNt[8] = "Eighty";
            SNt[9] = "Ninety";
            US[1] = "";
            US[2] = "Thousand";
            US[3] = "Million";
            US[4] = "Billion";
            US[5] = "Trillion";
            US[6] = "Quadrillion";
            US[7] = "Quintillion";
            US[8] = "Sextillion";
            US[9] = "Septillion";
            US[10] = "Octillion";
        }

        private void textBox2_TextChanged(object sender, EventArgs e)
        {

        }

        private void Form1_Load(object sender, EventArgs e)
        {
            Initialize();

        }

        private void label1_Click(object sender, EventArgs e)
        {

        }

    }

        
    }


** Edit ** code.gif
User is offlineProfile CardPM
+Quote Post

eclipsed4utoo
RE: Decimal To Words In C# Windows Form
14 Aug, 2008 - 04:45 AM
Post #2

D.I.C Regular
Group Icon

Joined: 21 Mar, 2008
Posts: 363



Thanked: 19 times
Dream Kudos: 25
My Contributions
you should check to make sure that the number of elements in the array is more than 1. what if the number does not contain a decimal? then it won't be split, and the array will only contain 1 element(index of 0). then when you say "no[1]", that value is null.
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 12/2/08 09:41PM

Live C# Help!

C# Tutorials

Reference Sheets

C# Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month