将数字形式转换为人民币大写形式的程序实现

本文介绍了一个Java程序,用于将阿拉伯数字转换成人民币大写的表示形式。程序通过一系列方法实现不同位数数字的转换,并考虑了特殊情况如零的处理。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

package org.phyeas.demo.number;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class NumberToRMB {
    
private static String[] UNIT = { """""亿" };
    
private static String[] UNIT2 = { """""" };

    
private static final String ZERO = "";

    
private static String[] NUM = { """""""""""""""",
            
"""" };

    
/**
     * 转换一串字符
     * 
     * 
@param str
     * 
@return
     
*/
    
public static String parseCharsToRMB(String str) {
        
if (!isNumber(str)) {
            
throw new IllegalArgumentException("参数不正确,必须为数字");
        }
        StringBuffer buffer 
= new StringBuffer();
        
int count = 0;
        
for (int i = str.length(); i > 0; i -= 4, count++) {
            String char4 
= null;
            
if (i - 4 < 0) {
                System.out.println(
"start=" + 0 + ".end=" + i);
                char4 
= str.substring(0, i);
                buffer.insert(
0, parse4CharsToRMB(char4) + UNIT[count]);
                
break;
            }
            System.out.println(
"start=" + (i - 4+ ".end=" + i);
            char4 
= str.substring(i - 4, i);
            buffer.insert(
0, parse4CharsToRMB(char4) + UNIT[count]);

        }
        
return buffer.toString();
    }

    
/**
     * 转换4位字符
     * 
     * 
@param str
     * 
@return
     
*/
    
public static String parse4CharsToRMB(String str) {
        
if (str.length() > 4 || !isNumber(str)) {
            
throw new IllegalArgumentException("参数不正确,str应该为不大于4位的字符");
        }
        StringBuffer result 
= new StringBuffer();
        
int startUNIT = getStartUNIT(str.length());

        
for (int i = 0; i < str.length(); i++) {
            
if (str.charAt(i) == '0') {
                
if (i == 0) {
                    result.append(ZERO);
                } 
else {
                    appendZero(result, i);
                }
                startUNIT
++;
            } 
else {
                
int index = Integer.parseInt(String.valueOf(str.charAt(i))) - 1;
                result.append(NUM[index]);
                
if (startUNIT < UNIT2.length) {
                    result.append(UNIT2[startUNIT]);
                    startUNIT
++;
                }
            }
        }
        
return result.toString();
    }

    
/**
     * 获取起始位置
     * 
     * 
@param strLength
     * 
@return
     
*/
    
public static int getStartUNIT(int strLength) {
        
switch (strLength) {
        
case 4:
            
return 0;
        
case 3:
            
return 1;
        
case 2:
            
return 2;
        
case 1:
            
return 3;
        
default:
            
return 0;
        }
    }

    
/**
     * 0是特殊的。所以单独一个方法添加0
     * 
     * 
@param buffer
     * 
@param index
     * 
@return
     
*/
    
private static StringBuffer appendZero(StringBuffer buffer, int index) {
        System.out.println(
"buffer=" + buffer);
        String before 
= buffer.substring(index, index + 1);
        System.out.println(
"before=" + before);
        
if (before.equals(ZERO)) {
            
return buffer;
        } 
else {
            buffer.append(ZERO);
        }
        
return buffer;
    }

    
/**
     * 判断是否为数字字符串
     * 
     * 
@param str
     * 
@return
     
*/
    
public static boolean isNumber(String str) {
        
if (str != null && str.length() > 0) {
            Pattern pattern 
= Pattern.compile("[0-9]*");
            Matcher m 
= pattern.matcher(str);
            
if (m.find()) {
                
return true;
            }
            
return false;
        }
        
return false;
    }

    
public static void main(String[] args) {
        System.out.println(
"转换1580654=" + parseCharsToRMB("1580654"));
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值