下面是我自己写的代码,仅供参考- import java.util.Scanner;
- public class Test10 {
- private static Scanner sc;
- public static void main(String[] args) {
- sc = new Scanner(System.in);
- // 0-12位的小数
- System.out.println("请输入要转换的数字(0-12位):");
- String str = sc.nextLine();
- // 将str转成long型然后对10000取余得到后四位
- long lo = Long.parseLong(str);
- long last = lo % 10000;
- // 同理可得中间四位以及前面四位
- long mid = lo / 10000 % 10000;
- long before = lo / 10000 / 10000 % 10000;
- // System.out.println(last + "\n" + mid + "\n" + before);
- // 调用方法得出结果
- System.out.println("转换后的结果为:" + method(before, mid, last));
- }
- // 可以得出最终结果的方法
- public static String method(long before, long mid, long last) {
- // TODO Auto-generated method stub
- // 定义一个空字符串
- String result = "";
- // System.out.println(before + "--" + mid + "--" + last);
- // 调用方法得出每一部分的中文输出
- String before1 = change(before);
- String mid1 = change(mid);
- String last1 = change(last);
- // 如果mid、before、last都为0
- if (before == 0 && mid == 0 && last == 0) {
- result = "零圆整";
- }
- // 判断如果mid小于等于4、last小于四位数且不为0,before为0
- else if (before == 0 && mid != 0 && last != 0 && String.valueOf(mid).length() <= 4
- && String.valueOf(last).length() < 4) {
- result = mid1 + "万" + "零" + last1 + "圆整";
- }
- // 判断如果mid小于等于4、last等于四位数且不为0,before为0
- else if (before == 0 && mid != 0 && last != 0 && String.valueOf(mid).length() <= 4
- && String.valueOf(last).length() == 4) {
- result = mid1 + "万" + last1 + "圆整";
- }
- // 判断如果before小于等于4且不为0、last小于等于四位数且不为0,mid为0
- else if (before != 0 && last != 0 && mid == 0 && String.valueOf(before).length() <= 4
- && String.valueOf(last).length() <= 4) {
- result = before1 + "亿" + "零" + last1 + "圆整";
- }
- // 判断如果before小于等于4且不为0、mid小于四位数且不为0,last为0
- else if (before != 0 && last == 0 && mid != 0 && String.valueOf(before).length() <= 4
- && String.valueOf(mid).length() < 4) {
- result = before1 + "亿" + "零" + mid + "万" + "圆整";
- }
- // 判断如果before小于等于4且不为0、mid等于四位数且不为0,last为0
- else if (before != 0 && last == 0 && mid != 0 && String.valueOf(before).length() <= 4
- && String.valueOf(mid).length() == 4) {
- result = before1 + "亿" + mid + "万" + "圆整";
- }
- // 如果mid小于等于四位数且不为0,last和before为0
- else if (String.valueOf(mid).length() <= 4 && mid != 0 && last == 0 && before == 0) {
- result = mid1 + "万" + "圆整";
- }
- // 如果before小于等于四位数且不为0,mid和last为0
- else if (String.valueOf(before).length() <= 4 && mid == 0 && last == 0 && before != 0) {
- result = before1 + "亿" + "圆整";
- }
- // 如果last小于等于四位数且不为0,mid和before为0
- else if (String.valueOf(last).length() <= 4 && mid == 0 && last != 0 && before == 0) {
- result = last1 + "圆整";
- }
- // 如果mid1不小于4位,last1小于四位
- else if (String.valueOf(mid).length() < 4) {
- result = before1 + "亿" + mid1 + "万" + "零" + last1 + "圆整";
- }
- // 否则都不添加零
- else {
- result = before1 + "亿" + mid1 + "万" + last1 + "圆整";
- }
- return result;
- }
- // 可以将不大于四位的数字汉字表达式输出的方法
- public static String change(long num) {
- // TODO Auto-generated method stub
- // 将long型转换层字符串型
- String str = String.valueOf(num);
- // 定义一个空字符串
- String result = "";
- // 定义两个数组用来存放中文数字汉字和单位
- String[] hanArr = { "零", "壹", "贰", "叁", "肆", "伍", "陆", "柒", "捌", "玖" };
- String[] unitArr = { "拾", "佰", "仟" };
- // 遍历
- for (int i = 0; i < str.length(); i++) {
- // 获取每一位的数字
- int numStr = str.charAt(i) - 48;
- // 如果不是最后一位数字且不是0则需要添加单位
- if (i != str.length() - 1 && numStr != 0) {
- result += hanArr[numStr] + unitArr[str.length() - 2 - i];
- }
- // 如果这个数字为0则输出空
- else if (num == 0) {
- result = "";
- }
- // 如果最后一个数字为零
- else if (i == str.length() - 1 && numStr == 0) {
- result += "";
- }
- // 否则不添加单位
- else {
- result += hanArr[numStr];
- }
- }
- // 遍历结果合并零
- if (result.contains("零零")) {
- result = result.replace("零零", "零");
- }
- return result;
- }
- }
复制代码
|
|