import java.util.Arrays;
public class Num2Rmb {
private String[] hanArr = { "零", "一", "二", "三", "四", "五", "六", "七", "八", "九" };
private String[] unitArr = { "千", "百", "十" };
// 价格数字分割为整数部分与小数部分
private String[] divide(double num) {
long zheng = (long) num;
long xiao = Math.round((num - zheng) * 100);
return new String[] { zheng + "", String.valueOf(xiao) };
}
// 将汉字转为字符串
private String toHanStr(String numStr) {
String result = "";
int Len = numStr.length();
for (int i = 0; i < Len; i++) {
int num = numStr.charAt(i) - '0';
// 如果不是只有一位则需要加单位
if (i != Len - 1 && num != 0)
result += hanArr[num] + unitArr[i];
else
result += hanArr[num];
}
return result;
}
public static void main(String[] args) {
Num2Rmb nr = new Num2Rmb();
System.out.println(Arrays.toString(nr.divide(123456.789)));
System.out.println(nr.toHanStr("6109"));
}
}
将汉字转为字符串
最新推荐文章于 2023-03-24 07:30:00 发布