import java.util.Scanner;
public class Num
{
public Num() {}
static String []bigNum={"零","一","二","三","四","五","六","七","八","九"};
static String getNUM(String str)
{
int t=Integer.parseInt(str);
return bigNum[t];
}
public static void main(String[] args)
{
try
{
String strbig=new String("");
System.out.print("請輸入數字:");
Scanner sc=new Scanner(System.in);
long num=sc.nextLong();
String temp=String.valueOf(num);
int b=temp.indexOf(".");
int s=temp.length()-(b+1);
int j=b;
for (int i =0; i<b;i++) {
strbig+=getNUM(temp.substring(i,i+1));
j--;
}
temp=temp.substring(b+1,temp.length());
for (int i = 0; i < s; i++)
{
strbig+=getNUM(temp.substring(i,i+1));
}
System.out.println("轉換結果:"+strbig);
}
catch(Exception ex)
{
System.out.println("請輸入整數");
}
}
}
B. 用java把阿拉伯數字轉換成中文數字,怎麼做
有個簡單的轉換,支持10億以內
public class Test {
public static void test(int d) {
// String[] str = { "零", "壹", "貳", "叄", "肆", "伍", "陸", "柒", "捌", "玖" };
String[] str = { "零", "一", "二", "三", "四", "五", "六", "七", "八", "九" };
// String ss[] = new String[] { "元", "拾", "佰", "仟", "萬", "拾", "佰", "仟", "億" };
String ss[] = new String[] { "個", "十", "百", "千", "萬", "十", "百", "千", "億" };
String s = String.valueOf(d);
System.out.println(s);
StringBuffer sb = new StringBuffer();
for (int i = 0; i < s.length(); i++) {
String index = String.valueOf(s.charAt(i));
sb = sb.append(str[Integer.parseInt(index)]);
}
String sss = String.valueOf(sb);
int i = 0;
for (int j = sss.length(); j > 0; j--) {
sb = sb.insert(j, ss[i++]);
}
System.out.println(sb);
}
public static void main(String[] args) {
test(3213);
test(912321321);
}
}
結果
不想要「個」的話就把 ss[] 數組中的「個」改成空
不過畢竟是機械的轉換,有零的話不太符合語言習慣