-1
A
回答
0
public class NumberToWord
{
private static String input;
private static int num;
private static String[] units=
{"",
" One",
" Two",
" Three",
" Four",
" Five",
" Six",
" Seven",
" Eight",
" Nine"
};
private static String[] teen=
{" Ten",
" Eleven",
" Twelve",
" Thirteen",
" Fourteen",
" Fifteen",
" Sixteen",
" Seventeen",
" Eighteen",
" Nineteen"
};
private static String[] tens=
{ " Twenty",
" Thirty",
" Forty",
" Fifty",
" Sixty",
" Seventy",
" Eighty",
" Ninety"
};
private static String[] maxs=
{"",
"",
" Hundred",
" Thousand",
" Lakh",
" Crore"
};
public String convertNumberToWords(int n)
{
input=numToString(n);
String converted="";
int pos=1;
boolean hun=false;
while(input.length()> 0)
{
if(pos==1) // TENS AND UNIT POSITION
{ if(input.length()>= 2) // TWO DIGIT NUMBERS
{
String temp=input.substring(input.length()-2,input.length());
input=input.substring(0,input.length()-2);
converted+=digits(temp);
}
else if(input.length()==1) // 1 DIGIT NUMBER
{
converted+=digits(input);
input="";
}
pos++;
}
else if(pos==2) // HUNDRED POSITION
{
String temp=input.substring(input.length()-1,input.length());
input=input.substring(0,input.length()-1);
if(converted.length()> 0&&digits(temp)!="")
{
converted=(digits(temp)+maxs[pos]+" and")+converted;
hun=true;
}
else
{
if
(digits(temp)=="");
else
converted=(digits(temp)+maxs[pos])+converted;hun=true;
}
pos++;
}
else if(pos > 2) // REMAINING NUMBERS PAIRED BY TWO
{
if(input.length()>= 2) // EXTRACT 2 DIGITS
{
String temp=input.substring(input.length()-2,input.length());
input=input.substring(0,input.length()-2);
if(!hun&&converted.length()> 0)
converted=digits(temp)+maxs[pos]+" and"+converted;
else
{
if(digits(temp)=="") ;
else
converted=digits(temp)+maxs[pos]+converted;
}
}
else if(input.length()==1) // EXTRACT 1 DIGIT
{
if(!hun&&converted.length()> 0)
converted=digits(input)+maxs[pos]+" and"+converted;
else
{
if(digits(input)=="") ;
else
converted=digits(input)+maxs[pos]+converted;
input="";
}
}
pos++;
}
}
return converted;
}
private String digits(String temp) // TO RETURN SELECTED NUMBERS IN WORDS
{
String converted="";
for(int i=temp.length()-1;i >= 0;i--)
{ int ch=temp.charAt(i)-48;
if(i==0&&ch>1 && temp.length()> 1)
converted=tens[ch-2]+converted; // IF TENS DIGIT STARTS WITH 2 OR MORE IT FALLS UNDER TENS
else if(i==0&&ch==1&&temp.length()==2) // IF TENS DIGIT STARTS WITH 1 IT FALLS UNDER TEENS
{
int sum=0;
for(int j=0;j < 2;j++)
sum=(sum*10)+(temp.charAt(j)-48);
return teen[sum-10];
}
else
{
if(ch > 0)
converted=units[ch]+converted;
} // IF SINGLE DIGIT PROVIDED
}
return converted;
}
private String numToString(int x) // CONVERT THE NUMBER TO STRING
{
String num="";
while(x!=0)
{
num=((char)((x%10)+48))+num;
x/=10;
}
return num;
}
private void inputNumber()
{
Scanner in=new Scanner(System.in);
try
{
System.out.print("Please enter number to Convert into Words : ");
num=in.nextInt();
}
catch(Exception e)
{
System.out.println("Number should be Less than 1 Arab ");
System.exit(1);
}
}
public static void main(String[] args)
{
NumberToWord obj=new NumberToWord();
obj.inputNumber();
System.out.println("input in Words : "+obj.convertNumberToWords(num));
}
}
相關問題
- 1. 轉換千分之一秒到h:mm:ss在moment.js
- 2. 價值到下一千而不是最接近的千分之一
- 3. 查看只顯示幾分之一秒
- 4. 從秒錶中返回十分之一秒百分之一秒
- 5. 轉換成十萬和千分之
- 6. 節省長/緯爲千分之一(軌跡)
- 7. 在排除數千個分隔符之前或之後匹配逗號,並排除數千個分隔符
- 8. bash du以小數點後的字節大小精確到千分之一
- 9. 分頁結果未顯示在第一頁之外
- 10. 千分隔符php
- 11. NSNumberFormatter千分隔符
- 12. 的DataGridViewCellStyle顯示千
- 13. 獲得第一百萬分之一秒
- 14. Facebook喜歡點擊顯示網址的幾分之一秒
- 15. HighChart:工具提示框千分位器
- 16. 如何在Android中顯示一半或四分之一的ImageView?
- 17. QSpinBox千位分隔符
- 18. Pandas.to_csv千個分隔符
- 19. 劃分數千個字段
- 20. 千位分隔符在Python
- 21. 千位分隔符printf
- 22. Apache poi - 千位分隔符
- 23. 格式千位分隔符
- 24. WPF ListView千分隔符
- 25. Devexpress XRtableCell千分隔符?
- 26. 十分之一秒的日期輸出?
- 27. Kendo UI Angular - kendo-numerictextbox - 顯示一年沒有千位分隔符的數字
- 28. 如何在extjs NumberFiled中顯示逗號或千分隔符?
- 29. PHP計數到千分之一的K風格計數像Facebook分享。 。 。 Twitter Button ect
- 30. 格式化一個asp:TextBox控件以及一千小數點分隔
還有,你試過嗎? – chrylis