JavaSE014_数组之应用举例(实现浮点数转换成人民币读法字符串)

实现浮点数转换成人民币读法字符串,思路如下:

int len = zhengStr.length();//取整数部分字符串长度

       for(i = 0; i < len; i++)//遍历整个整数字符串

           int temp = zhengStr.charAt(i) - 48;//当前字符转为数值

           int part = (len - i - 1) / 4;//当前字符位于那一段

           int location = (len - i - 1) % 4;//当前字符处于段的那一个位置

           if 当前字符不是本段最后一个

               if 当前字符不为‘0’    result += 转换成的繁体字 + 单位;    continue;

               else 当前字符为‘0’

                   if 当前字符是该段第一个字符并且result串的最后一个字符!='零' result += '零';continue;

                   else

                       if result串的最后一个字符为'零'    continue;

                       else result串的最后一个字符不为'零'    result += '零';

           else 当前字符是本段最后一个

               if 当前字符不为‘0’    result += 转换成的繁体字 + 段单位;    continue;

               else 当前字符为'0'

                   if result串的最后一个字符 != '零'    result += 段单位;    continue;

                   else resutl串的最后一个字符 == '零'

                       if result串的倒数第二个字符为'亿'或者‘万’    continue;

                       else result串的倒数第二个字符不为'亿'或者'万'  result+=result串去零+段单位;continue;

public class NumToRmb {

	private final String[] hanArr = { "零", "壹", "贰", "叁", "肆", "伍", "陆", "柒", "捌", "玖" };
	private final String[] unitArr = { "仟", "", "十", "佰" };
	private final String[] tag = { "元", "万", "亿" };

	public String toHanStr(double num) {

		String result = "";

		long zheng = (long) num;
		// long xiao = Math.round((num - zheng) * 100);//如果需要四舍五入,就用这个方法,但是小数点后两位依次为99时,第三位不能>=5,否则会报错
		long xiao = (long) ((num - zheng) * 100);// 如果不需要四舍五入,就用这个方法

		/* 整数部分为0单独考虑 */
		if (0 == zheng) {

			int tempJiao = (int) (xiao / 10);
			int tempFen = (int) (xiao % 10);

			if (tempJiao != 0) {
				result += hanArr[tempJiao] + "角";
			}
			if (tempFen != 0) {
				result += hanArr[tempFen] + "分";
			}

			return result;
		}

		String zhengStr = String.valueOf(zheng);
		int len = zhengStr.length();

		int tempLen = 0;

		/* 处理整数部分 */
		for (int i = 0; i < len; i++) {

			int temp = zhengStr.charAt(i) - 48;
			int part = (len - i - 1) / 4; // 当前字符处于哪个段
			int location = (len - i - 1) % 4; // 当前字符处于该段的具体哪个位置

			if (location != 0) { // 不是该段的最后一个

				if (temp != 0) { // 当前字符不为0
					result += hanArr[temp] + unitArr[(len - i) % 4];
					continue;
				} else { // 当前字符为0
					tempLen = result.length();
					if (3 == location && result.charAt(tempLen - 1) != '零') { // 当前字符为0且为该段第一个
						result += "零";
						continue;
					} else {
						tempLen = result.length();
						if (result.charAt(tempLen - 1) == '零') {
							continue;
						} else {
							result += "零";
							continue;
						}
					}
				}
			} else { // 是该段最后一个
				if (temp != 0) {
					result += hanArr[temp] + tag[part];
					continue;
				} else {
					tempLen = result.length();
					if (result.charAt(tempLen - 1) != '零') {
						result += tag[part];
						continue;
					} else {
						if (result.charAt(tempLen - 2) == '亿' || result.charAt(tempLen - 2) == '万') {
							continue;
						} else {
							result = result.substring(0, tempLen - 1) + tag[part];
							continue;
						}
					}
				}
			}

		}

		/* 处理小数部分 */
		int jiao = (int) (xiao / 10);
		int fen = (int) (xiao % 10);

		if (jiao != 0) {
			result += hanArr[jiao] + "角";
		}
		if (fen != 0) {
			result += hanArr[fen] + "分";
		}

		/* 排除输入形式如60 0000 0000.00,输出为“陆十亿零”的情况 */
		tempLen = result.length();
		if (result.charAt(tempLen - 1) == '零') {
			result = result.substring(0, tempLen - 1);
		}
		tempLen = result.length();
		if (result.charAt(tempLen - 1) == '元') {
			result += "整";
		}
		if (result.charAt(tempLen - 1) == '万' || result.charAt(tempLen - 1) == '亿') {
			result += "元整";
		}

		return result;
	}

	public static void main(String[] args) {

		NumToRmb ntr = new NumToRmb();
		System.out.println(ntr.toHanStr(6000000000.0000));
		System.out.println(ntr.toHanStr(600100.000));
		System.out.println(ntr.toHanStr(1006.333));
		System.out.println(ntr.toHanStr(0.0333));
		System.out.println(ntr.toHanStr(0.0373));//输出:叁分。没考虑四舍五入
		System.out.println(ntr.toHanStr(0.0763));
	}
}
输出结果为:

陆十亿元整
陆十万零壹佰元整
壹仟零陆元叁角叁分
叁分
叁分
柒分

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值