美文网首页大数据Java 杂谈
在java中如何把阿拉伯数字转化为中文读法(面试算法题)

在java中如何把阿拉伯数字转化为中文读法(面试算法题)

作者: Java程序员YY | 来源:发表于2019-08-16 16:29 被阅读3次

    重点在于0的处理,还有繁杂的字符、字符串、整数之间的转化,那些方法要记牢。

    package algorithm;

    import java.util.*;

    public class Solution {

    static char[] numArr = {'零','一','二','三','四','五','六','七','八','九'};

    static String[] unitArr = {"","十","百","千","万"};

    public static String numToChinese(int num) {

    char[] newNum = String.valueOf(num).toCharArray();

    int len = newNum.length;

    StringBuilder res = new StringBuilder();

    for(int i=0;i

    int temp = Integer.parseInt(String.valueOf(newNum[i]));

    boolean zeroFlag = temp==0 ? true : false;

    if(zeroFlag) {

    while(i

    i++;

    if(i!=len-1) {

    res.append(numArr[0]);

    }

    }else {

    res.append(numArr[temp]);

    res.append(unitArr[len-i-1]);

    }

    }

    return res.toString();

    }

    public static void main(String[] args) {

    System.out.print(numToChinese(12345));

    System.out.print("\n");

    System.out.print(numToChinese(10000));

    System.out.print("\n");

    System.out.print(numToChinese(10001));

    System.out.print("\n");

    System.out.print(numToChinese(10100));

    System.out.print("\n");

    System.out.print(numToChinese(10101));

    System.out.print("\n");

    System.out.print(numToChinese(20890));

    }

    }

    最新免费java,架构,大数据AI编程资料获取添加

    薇信:18410263200

    通过验证填写“111”(备注必填)

    相关文章

      网友评论

        本文标题:在java中如何把阿拉伯数字转化为中文读法(面试算法题)

        本文链接:https://www.haomeiwen.com/subject/pvjqsctx.html