java 数字转大写_Java 阿拉伯数字转换为中文大写数字

这个Java类实现了将阿拉伯数字转换为中文大写数字的功能,包括整数和小数部分。它可以处理不同类型的数字输入,并且可以添加单位和后缀。

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

/**

*

*

*

Copyright 1994 JsonInternational

*

All rights reserved.

*

Created on 19941115

*

Created by Jason

*

*

*/

package cn.ucaner.alpaca.framework.utils.chinese;

import java.text.DecimalFormat;

import java.util.HashMap;

import java.util.Map;

import cn.ucaner.alpaca.framework.utils.string.StringHelper;

/**

* @Package:cn.ucaner.framework.utils

* @ClassName:ChineseNumber

* @Description:

阿拉伯数字转换为中文大写数字.

* @Author: - Jason

* @CreatTime:2017年8月30日 下午2:15:16

* @Modify By:

* @ModifyTime:

* @Modify marker:

* @version V1.0

*/

public class ChineseNumber {

private static final String[] BEFORE_SCALE = { "万", "仟", "佰", "拾", "亿", "仟", "佰", "拾", "万", "仟", "佰", "拾", "" };

private static final String[] AFTER_SCALE = { "角", "分" };

private static final String DEFAULT_PATH_SEPARATOR = ".";

private static final Map NUMBER_MAPPING = new HashMap();

static {

NUMBER_MAPPING.put("0", "零");

NUMBER_MAPPING.put("1", "壹");

NUMBER_MAPPING.put("2", "贰");

NUMBER_MAPPING.put("3", "叁");

NUMBER_MAPPING.put("4", "肆");

NUMBER_MAPPING.put("5", "伍");

NUMBER_MAPPING.put("6", "陆");

NUMBER_MAPPING.put("7", "柒");

NUMBER_MAPPING.put("8", "捌");

NUMBER_MAPPING.put("9", "玖");

}

public static String getChineseNumber(String number) {

return getChineseNumber(number, null, null);

}

public static String getChineseNumber(String number, String unit, String postfix) {

String[] numbers = StringHelper.strToStrArray(number, DEFAULT_PATH_SEPARATOR);

if (numbers.length > 2) {

new NumberFormatException("数字格式错误!");

}

int length = numbers[0].length();

int isZero = 0;

StringBuffer result = new StringBuffer();

for (int i = 0; i < length; i++) {

String digit = String.valueOf(numbers[0].charAt(i));

boolean allZero = true; // 如果后继的全部是零,则跳出

for (int j = i; j < length; j++) {

if (numbers[0].charAt(j) != '0') {

allZero = false;

break;

}

}

if (allZero) {

boolean hasValue = false;

for (int z = i; z >= 0; z--) {

if (numbers[0].charAt(z) != '0' && length - z <= 7 && length - z >= 5) {

hasValue = true;

break;

}

}

// 加万单位

if ( ( length - i > 4 && length <= 8 ) || ( hasValue && length - i > 4 )) {

result.append(BEFORE_SCALE[8]);

}

// 加亿单位

if (length - i >= 9) {

result.append(BEFORE_SCALE[4]);

}

break;

}

if (length < 9 && length - i == 5) {

if (!"0".equals(digit) && isZero > 0) {

result.append(NUMBER_MAPPING.get("0"));

}

if ("0".equals(digit)) {

result.append(BEFORE_SCALE[8]);

if (isZero > 0) {

result.append(NUMBER_MAPPING.get("0"));

}

continue;

}

}

if ("0".equals(digit) && length > 9 && length - i == 9) {

result.append(BEFORE_SCALE[4]);

continue;

}

if (isZero < 1 || !"0".equals(digit)) {

if ("0".equals(digit)) {

if (length - i != 6 && length - i != 7) {

result.append(NUMBER_MAPPING.get(digit));

}

} else {

result.append(NUMBER_MAPPING.get(digit));

}

if (!"0".equals(digit)) {

result.append(BEFORE_SCALE[BEFORE_SCALE.length - length + i]);

}

}

if ("0".equals(digit)) {

isZero++;

} else {

isZero = 0;

}

}

result.append(unit == null ? "圆" : result.append(unit));

if (numbers.length == 1) {

result.append(postfix == null ? "整" : result.append(postfix));

return result.toString();

}

length = numbers[1].length();

for (int j = 0; j < length; j++) {

if (j > 2) {

break;

}

if (numbers[1].charAt(j) == '0') {

continue;

}

result.append(NUMBER_MAPPING.get(String.valueOf(numbers[1].charAt(j))));

result.append(AFTER_SCALE[j]);

}

result.append(postfix == null ? "整" : result.append(postfix));

return result.toString();

}

public static String getChineseNumber(int number) {

return getChineseNumber(new Integer(number));

}

public static String getChineseNumber(int number, String unit, String postfix) {

return getChineseNumber(new Integer(number), unit, postfix);

}

public static String getChineseNumber(Long number) {

return getChineseNumber(number.toString(), null, null);

}

public static String getChineseNumber(Integer number) {

return getChineseNumber(number.toString(), null, null);

}

public static String getChineseNumber(Integer number, String unit, String postfix) {

return getChineseNumber(number.toString(), unit, postfix);

}

public static String getChineseNumber(Long number, String unit, String postfix) {

return getChineseNumber(number.toString(), unit, postfix);

}

public static String getChineseNumber(long number) {

return getChineseNumber(new Long(number));

}

public static String getChineseNumber(long number, String unit, String postfix) {

return getChineseNumber(new Long(number), unit, postfix);

}

public static String getChineseNumber(double number, String unit, String postfix) {

DecimalFormat f = (DecimalFormat) DecimalFormat.getInstance();

f.applyLocalizedPattern("#.##");

return getChineseNumber(f.format(number), unit, postfix);

}

public static String getChineseNumber(double number) {

return getChineseNumber(number, null, null);

}

public static String getChineseNumber(Double number) {

return getChineseNumber(number.doubleValue());

}

public static String getChineseNumber(Double number, String unit, String postfix) {

return getChineseNumber(number.doubleValue(), unit, postfix);

}

public static void main(String[] args) {

System.out.println(getChineseNumber(1994));

System.out.println(getChineseNumber(1994.1115));

System.out.println(getChineseNumber(19941115));

}

}

//Outputs

//壹仟玖佰玖拾肆圆整

//壹仟玖佰玖拾肆圆壹角壹分整

//壹仟玖佰玖拾肆万壹仟壹佰壹拾伍圆整

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值