数字转中文数字(123–>“壹佰贰拾叁”、“一百二十三”)

直接上代码,注释完整,各位网友如有其它好的方案,麻烦留言探讨,万分感谢!

package fun.chice.xxx;/*** 数字转中文数字工具* Created by Chice on 2018/9/5.* Email:2206143885@qq.com* CSDN:http://blog.csdn.net/chicet*/public final class NumToChineseLetterFormatter {private static final String CHINESE_NUM_FLAG_NEGATIVE = "负";private static final String CHINESE_NUM_FLAG_ZERO = "零";private static final String CHINESE_NUM_FLAG_DOUBLE_ZERO = "零零";private static final String CHINESE_NUM_FLAG_WAN = "万";private static final String CHINESE_NUM_FLAG_YI = "亿";private static final String CHINESE_NUM_FLAG_ZHAO = "兆";// 此数位存在争议,暂用“亿亿为兆”的方案private static final String[] CHINESE_NUM_UNIT = {"", "十", "百", "千"};private static final String[] CHINESE_NUM = {"零", "一", "二", "三", "四", "五", "六", "七", "八", "九"};private static final String[] CHINESE_NUM_UNIT_OF_ACCOUNTANT = {"", "拾", "佰", "仟"};private static final String[] CHINESE_NUM_OF_ACCOUNTANT = {"零", "壹", "贰", "叁", "肆", "伍", "陆", "柒", "捌", "玖"};private static boolean isRemoveOneBeforeTenUnit = false;private static boolean isUseAccountantStyle = false;/*** <p>将数据转换成中文数字字符串</p>* <p>思路:判断正负-->四位分割-->四位转化-->拼接(按位添加位符号)-->移除前缀零、移除后缀零-->移除重复零-->输出</p>** @param num                  数字* @param isUseAccountantStyle 是否使用会计格式(会计格式用中文大写)* @return 中文数字*/public static String getNumInChineseLetter(long num, boolean isUseAccountantStyle) {if (num == 0) {return CHINESE_NUM_FLAG_ZERO;}StringBuilder builder = new StringBuilder();if (num < 0) {builder.append(CHINESE_NUM_FLAG_NEGATIVE);num = -num;}NumToChineseLetterFormatter.isUseAccountantStyle = isUseAccountantStyle;isRemoveOneBeforeTenUnit = (!isUseAccountantStyle && num < 20 && num > 9);getNumToChinese(builder, num);// 滤掉前缀零while (builder.charAt(0) == CHINESE_NUM_FLAG_ZERO.charAt(0)) {builder.deleteCharAt(0);}// 滤掉后缀零while (builder.length() > 0 && builder.charAt(builder.length() - 1) == CHINESE_NUM_FLAG_ZERO.charAt(0)) {builder.deleteCharAt(builder.length() - 1);}// 替换掉重复零int indexOfDoubleZero = builder.indexOf(CHINESE_NUM_FLAG_DOUBLE_ZERO);while (indexOfDoubleZero > -1) {builder.replace(indexOfDoubleZero, indexOfDoubleZero + 2, CHINESE_NUM_FLAG_ZERO + "");indexOfDoubleZero = builder.indexOf(CHINESE_NUM_FLAG_DOUBLE_ZERO);}return builder.toString();}/*** 把数字按位转为中文数字(尚未格式化)** @param builder stringBuilder* @param num     num*/private static void getNumToChinese(StringBuilder builder, long num) {int numLength = ("" + num).length();int numSplitCountBy4Length = (numLength + 3) / 4;for (int i = 0; i < numSplitCountBy4Length; i++) {int splitedNum = (int) (num / (long) (Math.pow(10, 4 * (numSplitCountBy4Length - i - 1))));num = (num % (long) (Math.pow(10, 4 * (numSplitCountBy4Length - i - 1))));getChinesForNumLessThan10000(builder, splitedNum);if (splitedNum >= 0) {if (i < (numSplitCountBy4Length - 4) && (numSplitCountBy4Length - i) % 4 == 1) {builder.append(CHINESE_NUM_FLAG_ZHAO);} else if (i < (numSplitCountBy4Length - 2) && (numSplitCountBy4Length - i) % 2 == 1) {builder.append(CHINESE_NUM_FLAG_YI);} else {if (i < numSplitCountBy4Length - 1)builder.append(CHINESE_NUM_FLAG_WAN);}}}}/*** 对小于一万的数字进行转换** @param builder stringBuilder* @param num     num*/private static void getChinesForNumLessThan10000(StringBuilder builder, int num) {if (num == 0) {builder.append(isUseAccountantStyle ? CHINESE_NUM_OF_ACCOUNTANT[0] : CHINESE_NUM[0]);return;}num = num % 10000;for (int i = 0; i < 4; i++) {int numInOrder = num / (int) Math.pow(10, 3 - i);num = num % (int) Math.pow(10, 3 - i);if (i == 3 && numInOrder == 0) break;if (isRemoveOneBeforeTenUnit && i == 2 && numInOrder == 1) {// 这里移除十位前面的一(一十--->十)// 为了便于理解,这个空if暂且保留} else {builder.append(isUseAccountantStyle ? CHINESE_NUM_OF_ACCOUNTANT[numInOrder] : CHINESE_NUM[numInOrder]);}if (numInOrder > 0)builder.append(isUseAccountantStyle ? CHINESE_NUM_UNIT_OF_ACCOUNTANT[3 - i] : CHINESE_NUM_UNIT[3 - i]);}}
}

Java 数字转中文数字(会计格式与非会计格式,暂不包含小数)相关推荐

  1. c# 数字转换为中文数字

    //数字转换为中文public string GetCountRefundInfoInChanese(string inputNum){string[] intArr = { "0" ...

  2. js工具函数之数字转为中文数字和大写金额

    1. 数字转为中文数字 function numberToChinese(num) {var AA = new Array("零", "一", "二& ...

  3. php数字转中文数字排序,php实现中文转数字

    分享一个辅助函数,使用php尽可能识别出字符串中的数字, 先上代码 function checkNatInt($str) { $map = array( '一' => '1','二' => ...

  4. 数字转中文数字与中文数字转数字

    利用数组与map的数据结构进行中文数字与数字之间的互相转换 public class NumberUtil {private NumberUtil() { }private static final ...

  5. java统计string中文数字英文_Java学习(4):统计一个文件中的英文,中文,数字,其他字符以及字符总数...

    import java.io.BufferedReader; import java.io.File; import java.io.FileInputStream; import java.io.F ...

  6. java中文转换数字_Java 中文数字转换为阿拉伯数字

    贴出代码,方便学习交流,稍后放出镜像问题的代码 package com.thunisoft.cail.utils; import com.sun.istack.internal.NotNull; im ...

  7. java--阿拉伯数字转中文数字

    写在前面 该方法目前只能处理Integer能够装得下的值,已经处理了[零...]的情况 有实现思路,仅供参考,发现问题可调优的地方多谢指正 上菜!!! package com.xxx.util;imp ...

  8. Javscript 实现字符数串比对排序(包含数字及中文数字)

    主要代码: /** *@description: 比较两个字符串大小 *@author: JackieZheng *@date: 2020-12-03 19:59:39 */ function com ...

  9. jq 数字转中文数字_阿拉伯数字 转换 中文大写

    阿拉伯数字 转换 中文大写 class ToChineseNumber { private $money = ""; private $cnynums = array(" ...

最新文章

  1. ASP.NET Web API 过滤器创建、执行过程(二)
  2. markdown编辑器基本用法
  3. 前端学习笔记day01 html 标签之音频 embed+audio+video
  4. 数组动态初始化【应用】
  5. Linux下读取smBIOS源码,Linux下读取SMBIOS信息
  6. 5G商用将满一年,6G研发开始了...
  7. WPF: 本地化(Localization) 实现
  8. python求一元三次方程的根_关于二次、三次、四次方程求解方法讨论
  9. matlab分析xml文件_修改Java中的XML文件(DOM分析器)
  10. LDA线性判别原理解析<数学推导>
  11. 软考高项-质量管理论文范文
  12. 免费数据集下载(很全面)
  13. web打印三种实现方式
  14. 2022-2027年中国SPA水疗行业市场调研及未来发展趋势预测报告
  15. 基于HTML5的在线绘图工具,基于HTML5 Canvas和jQuery 的绘图工具的实现
  16. 【阿里云镜像】使用VM虚拟机安装OpenWRT并更换阿里云镜像源
  17. java 线程的构造函数_深入理解Thread构造函数
  18. n-null is null: method kotlin.jvm.internal.Intrinsics.checkParameterIsNotNull, parameter convertView
  19. h5活动是什么意思_h5是什么(H5和Html5是一个东西吗)
  20. jsp+ssm计算机毕业设计中青年健康管理监测系统【附源码】

热门文章

  1. 【统计】时间序列预测之 Holt-Winters 指数平滑模型
  2. 初步的解了Pathon
  3. Kubernetes学习-K8S安装篇-Kubeadm安装高可用K8S集群
  4. Pandas 实用技能,数据筛选 query 函数详细介绍
  5. Incorrect string value: '\xF0\x9F\x98\x82\xF0\x9F...'
  6. 人生进度条百分之20_1分钟get技能:缺了“进度条”,你注定和80%的失败者一样实现不了人生目标...
  7. INTERN: A New Learning Paradigm Towards General Vision
  8. FP-Tree算法的实现
  9. 音视频开发工程师学习之路
  10. 卖虾滑的鲜美来IPO:水产品预制菜第一股,蹭热度or真有料?