ChineseNumber 转换
中文数字转换
/**
* <html>
* <body>
* <P> Copyright 1994 JsonInternational</p>
* <p> All rights reserved.</p>
* <p> Created on 19941115</p>
* <p> Created by Jason</p>
* </body>
* </html>
*/
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: <p> 阿拉伯数字转换为中文大写数字. </p>
* @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<String, String> NUMBER_MAPPING = new HashMap<String, String>();
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
//壹仟玖佰玖拾肆圆整
//壹仟玖佰玖拾肆圆壹角壹分整
//壹仟玖佰玖拾肆万壹仟壹佰壹拾伍圆整
ChineseNumber 转换的更多相关文章
- C#日期转换类
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Te ...
- javascript中的Array对象 —— 数组的合并、转换、迭代、排序、堆栈
Array 是javascript中经常用到的数据类型.javascript 的数组其他语言中数组的最大的区别是其每个数组项都可以保存任何类型的数据.本文主要讨论javascript中数组的声明.转换 ...
- opencv中Mat与IplImage,CVMat类型之间转换
opencv中对图像的处理是最基本的操作,一般的图像类型为IplImage类型,但是当我们对图像进行处理的时候,多数都是对像素矩阵进行处理,所以这三个类型之间的转换会对我们的工作带来便利. Mat类型 ...
- ASP.NET_各个币种之间的汇率转换(实时)使用Yahoo汇率。
近期开发支付平台的时候有运用到各国的实时汇率之间的转换问题,于是在往上找了很多相关资料,以下就是一些参考网址: 1.提供API接口的网站:https://www.showapi.com:这个网站有提供 ...
- Taurus.MVC 2.2 开源发布:WebAPI 功能增强(请求跨域及Json转换)
背景: 1:有用户反馈了关于跨域请求的问题. 2:有用户反馈了参数获取的问题. 3:JsonHelper的增强. 在综合上面的条件下,有了2.2版本的更新,也因此写了此文. 开源地址: https:/ ...
- XStream将java对象转换为xml时,对象字段中的下划线“_”,转换后变成了两个的解决办法
在前几天的一个项目中,由于数据库字段的命名原因 其中有两项:一项叫做"市场价格"一项叫做"商店价格" 为了便于区分,遂分别将其命名为market ...
- Android中手机录屏并转换GIF的两种方式
之前在博文中为了更好的给大家演示APP的实现效果,本人了解学习了几种给手机录屏的方法,今天就给大家介绍两种我个人用的比较舒服的两种方法: (1)配置adb环境后,使用cmd命令将手机界面操作演示存为视 ...
- React的使用与JSX的转换
前置技能:Chrome浏览器 一.拿糖:React的使用 React v0.14 RC 发布,主要更新项目: 两个包: React 和 React DOM DOM node refs 无状态的功能 ...
- WebForm获取GET或者POST参数到实体的转换,ADO.NET数据集自动转换实体
最近在修改维护以前的webform项目(维护别人开发的.....)整个aspx没有用到任何的控件,这个我也比较喜欢不用控件所以在提交信息的时候需要自己手动的去Request.QueryString[] ...
随机推荐
- MVC框架模式和Javaweb经典三层架构
一.MVC设计模式 1.MVC的概念 首先我们需要知道MVC模式并不是javaweb项目中独有的,MVC是一种软件工程中的一种软件架构模式,把软件系统分为三个基本部分:模型(Model).视图(Vie ...
- 查看Linux机器的外网IP
curl icanhazip.comcurl ifconfig.mecurl curlmyip.comcurl ip.appspot.comcurl ipinfo.io/ipcurl ipecho.n ...
- 刷题记录:[De1CTF 2019]Giftbox && Comment
目录 刷题记录:[De1CTF 2019]Giftbox && Comment 一.知识点 1.sql注入 && totp 2.RCE 3.源码泄露 4.敏感文件读取 ...
- wordpress 本地环境安装
1. 下载xmapp 2. 安装mysql 3. 启动xmapp的数据库与Apache,通常无法启动原因. mac的先关闭自己的数据库,系统偏好设置-MySQL Apache的端口默认是80,一般会被 ...
- openwrt如何打开linux内核的CONFIG_DEVMEM选项?
答: 直接在openwrt的make menuconfig中打开CONFIG_KERNEL_DEVMEM选项即可
- C# .net 高清压缩图片 合并图片方法
/// <summary> /// 合并宽度一样的图片 /// </summary> /// <param name="imgUrls">多张图 ...
- 爬虫中采集动态HTML介绍
JavaScript JavaScript 是网络上最常用也是支持者最多的客户端脚本语言.它可以收集 用户的跟踪数据,不需要重载页面直接提交表单,在页面嵌入多媒体文件,甚至运行网页游戏. 我们可以在网 ...
- 【转】Revit二次开发——读取cad中的文字信息
Revit读取cad的文字信息需要借助Teigha的开源dll,在程序中添加下图中红色框的dll文件的引用,其他的dll文件全部放在同一个文件夹中即可,运行的时候,会自动把这些dll文件全部复制到bi ...
- java面试题实战二
1.spring 是如何创建bean的? 在IoC容器中,bean的获取主要通过BeanFactory和ApplicationContext获取,这里ApplicationContext实际上是继承自 ...
- IDEA代码折叠
IDEA代码折叠 觉得有用的话,欢迎一起讨论相互学习~Follow Me 选中内容-->右键 folding -->fold selection/remove region