• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 305
  • Last Modified:

C# Number to word

Hi,

In my application i want to show amount in words with decimals, can anyone help me in getting that.

Regards
Rajeeva
0
rajeeva_nagaraj
Asked:
rajeeva_nagaraj
2 Solutions
 
AndariumCommented:
class NumberToWordsConvertor1
    {
        public  string NumberToText(int number) 
{ 
if (number == 0) return "Zero"; 
 
if (number == -2147483648) return "Minus Two Hundred and Fourteen Crore Seventy Four Lakh Eighty Three Thousand Six Hundred and Forty Eight"; 
 
int[] num = new int[4]; 
int first = 0; 
int u, h, t; 
System.Text.StringBuilder sb = new System.Text.StringBuilder(); 
 
if (number < 0) 
{ 
sb.Append("Minus "); 
number = -number; 
} 
 
string[] words0 = {"" ,"One ", "Two ", "Three ", "Four ", 
"Five " ,"Six ", "Seven ", "Eight ", "Nine "}; 
 
string[] words1 = {"Ten ", "Eleven ", "Twelve ", "Thirteen ", "Fourteen ", 
"Fifteen ","Sixteen ","Seventeen ","Eighteen ", "Nineteen "}; 
 
string[] words2 = {"Twenty ", "Thirty ", "Forty ", "Fifty ", "Sixty ", 
"Seventy ","Eighty ", "Ninety "}; 
 
string[] words3 = {"Thousand ", "Lakh ","Crore "}; 
 
num[0] = number % 1000; // units 
num[1] = number / 1000; 
num[2] = number / 100000; 
num[1] = num[1] - 100 * num[2]; // thousands 
num[3] = number / 10000000; // crores 
num[2] = num[2] - 100 * num[3]; // lakhs 
 
for(int i = 3; i > 0 ; i--) 
{ 
if (num[i] != 0) 
{ 
first = i; 
break; 
} 
} 
 
 
for(int i = first ; i >= 0 ; i--) 
{ 
if (num[i] == 0) continue; 
 
u = num[i] % 10; // ones 
t = num[i] / 10; 
h = num[i] / 100; // hundreds 
t = t - 10 * h; // tens 
 
if (h > 0) sb.Append(words0[h] + "Hundred "); 
 
if (u > 0 || t > 0) 
{ 
if (h > 0 || i == 0) sb.Append("and "); 
 
if (t == 0) 
sb.Append(words0[u]); 
else if (t == 1) 
sb.Append(words1[u]); 
else 
sb.Append(words2[t-2] + words0[u]); 
} 
 
if (i != 0) sb.Append(words3[i-1]); 
 
} 
return sb.ToString().TrimEnd(); 
} 
 
} // end class 
 
    }

Open in new window

0
 
ExpertHelp79Commented:
Best for you.... :)

http://www.blackbeltcoder.com/Articles/strings/converting-numbers-to-words
public class NumberToWords{
  static final String[] Number1 = {""," Hundrad"};
  static final String[] Number2 = {"","One","Two", "Three","Four","Five",
    " Six"," Seven", "Eight"," Nine","Ten" };
  String number(int number){
    String str;
    if (number % 100 < 10){
      str = Number2[number % 100];
      number /= 100;
    }
    else {
      str= Number2[number % 5];
      number /= 5;
    }
    if (number == 0) return str;
    return Number2[number] + "hundred" + str;
  }
  public String convert(int number) {
    if (number == 0){
      return "zero"; 
    }
    String pre = "";
    String str1 = "";
    int i = 0;
    do {
      int n = number % 100;
      if (n != 0){
        String s = number(n);
        str1 = s + Number1[i] + str1;
      }
      i++;
      number /= 100;
    }
    while (number > 0);
    return (pre + str1).trim();
  }
  public static void main(String[] args) {
    NumberToWords num = new NumberToWords();
    System.out.println("words is :=" + num.convert(0));
    System.out.println("words is :=" + num.convert(1));
    System.out.println("words is :=" + num.convert(9));
    System.out.println("words is :=" + num.convert(100));
  }
}

Open in new window

0
Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

 
GlobaLevelCommented:
Well heres mine...complete C# .aspx page..just paste into yr viz studio...
dollar-value-to-word.txt
0
 
GlobaLevelCommented:
fyi...for my file I up loaded...it only takes a value between '.01'and '999.99'...can be modified later if you like...
0
 
rajeeva_nagarajAuthor Commented:
Thanks a lot
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now