问题描述
我想将数字转换为数字转换为单词英语指标说百万等。目前我有一个代码转换为印度卢比格式。我想用英语指标表格。
我的代码如下:
I want to convert amount in digits to wordsin English Metrics say million etc. Currently I have a code to convert in Indian Rupee format. I want in English Metrics form.
My code is as below:
public static string NumbersToWords(int inputNumber)
{
int inputNo = inputNumber;
if (inputNo == 0)
return "Zero";
int[] numbers = new int[4];
int first = 0;
int u, h, t;
System.Text.StringBuilder sb = new System.Text.StringBuilder();
if (inputNo < 0)
{
sb.Append("Minus ");
inputNo = -inputNo;
}
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 " };
numbers[0] = inputNo % 1000; // units
numbers[1] = inputNo / 1000;
numbers[2] = inputNo / 100000;
numbers[1] = numbers[1] - 100 * numbers[2]; // thousands
numbers[3] = inputNo / 10000000; // crores
numbers[2] = numbers[2] - 100 * numbers[3]; // lakhs
for (int i = 3; i > 0; i--)
{
if (numbers[i] != 0)
{
first = i;
break;
}
}
for (int i = first; i >= 0; i--)
{
if (numbers[i] == 0) continue;
u = numbers[i] % 10; // ones
t = numbers[i] / 10;
h = numbers[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();
}
推荐答案
//1000 = Thousand
//1000,000 = Million
//1000,000,000 - Billion
//etc...
units = number % 1000
thousands = number / 1000
tUnits = thousands % 1000
millions = thousands / 1000
mUnits = millions % 1000
//treat units, tUnits and mUnits the same:
string result = "";
if(millions>0){
result += UnitString(mUnits)
result += " million"
}
if(Thousands>0){
result += UnitString(mUnits)
result += " thousand"
}
if(number>0){
result += UnitString(mUnits)
}
...
private string UnitsString(int Units){
// deal with 100's, 10's and units
}
...
所以数字应该是这样的:
10,000,000 =一千万
12,123,123 =一千二百万一百零二十hree一千一百二十三
所有英语(阿拉伯语)数字的数字都是相同的。
10 ^(3 * 1)= 10 ^ 3 =千= 1,000
10 ^(3 * 2)= 10 ^ 6 =百万= 1,000,000
10 ^(3 * 3)= 10 ^ 9 =十亿= 1,000,000,000
10 ^(3 * 4)= 10 ^ 12 =万亿= 1,000,000,000,000
10 ^ (3 * 5)= 10 ^ 15 = Quadrillion = 1,000,000,000,000,000
10 ^(3 * 6)= 10 ^ 18 = Quintilian = 1,000,000,000,000,000,000
希望有帮助
Andy ^ _ ^
so the numbers should turn out like this:
10,000,000 = "Ten million"
12,123,123 = "Twelve million One hundred and Twenty Three Thousand One hundred and Twenty Three"
The same pattern is true of all English (Arabic) numbers of any magnitude.
10^(3*1) = 10^3 = Thousand = 1,000
10^(3*2) = 10^6 = Million = 1,000,000
10^(3*3) = 10^9 = Billion = 1,000,000,000
10^(3*4) = 10^12 = Trillion = 1,000,000,000,000
10^(3*5) = 10^15 = Quadrillion = 1,000,000,000,000,000
10^(3*6) = 10^18 = Quintilian = 1,000,000,000,000,000,000
Hope that helps
Andy ^_^
这篇关于如何将数字量转换为英语指标中的单词?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!