货币 和 日期 转 中文大写
货币数值转中文大写
import java.math.BigDecimal;
/**
* 阿拉伯数字转中文大写
*/
public class MoneyToUpperChinese {
/** 大写数字 */
private static final String[] NUMBERS = { "零", "壹", "贰", "叁", "肆", "伍", "陆", "柒", "捌", "玖" };
/** 整数部分的单位 */
private static final String[] IUNIT = { "元", "拾", "佰", "仟", "万", "拾", "佰", "仟", "亿", "拾", "佰", "仟", "万", "拾", "佰", "仟" };
/** 小数部分的单位 */
private static final String[] DUNIT = { "角", "分", "厘" };
/**
* 得到大写金额。
*/
public synchronized static String toChinese(String str) {
boolean isNegative = false;
if (str.startsWith("-")) {
isNegative = true;
str = str.substring(1);
}
Float f = Float.parseFloat(str);
if (f.intValue() == 0) {
return "零元整";
}
str = str.replaceAll(",", "");// 去掉","
String integerStr;// 整数部分数字
String decimalStr;// 小数部分数字
// 初始化:分离整数部分和小数部分
if (str.indexOf(".") > 0) {
integerStr = str.substring(0, str.indexOf("."));
decimalStr = str.substring(str.indexOf(".") + 1);
} else if (str.indexOf(".") == 0) {
integerStr = "";
decimalStr = str.substring(1);
} else {
integerStr = str;
decimalStr = "";
}
// integerStr去掉首0,不必去掉decimalStr的尾0(超出部分舍去)
if (!integerStr.equals("")) {
integerStr = Long.toString(Long.parseLong(integerStr));
if (integerStr.equals("0")) {
integerStr = "";
}
}
// overflow超出处理能力,直接返回
if (integerStr.length() > IUNIT.length) {
System.out.println(str + ":超出处理能力");
return str;
}
int[] integers = toArray(integerStr);// 整数部分数字
boolean isMust5 = isMust5(integerStr);// 设置万单位
int[] decimals = toArray(decimalStr);// 小数部分数字
if (isNegative) {
return "负" + getChineseInteger(integers, isMust5) + getChineseDecimal(decimals);
} else {
return getChineseInteger(integers, isMust5) + getChineseDecimal(decimals);
}
}
/**
* 得到大写金额 double
*/
public synchronized static String toChinese(double doubleStr) {
return toChinese(String.valueOf(doubleStr));
}
/** 得到大写金额 BigDecimal */
public synchronized static String toChinese(BigDecimal bd) {
return toChinese(bd.toString());
}
/**
* 整数部分和小数部分转换为数组,从高位至低位
*/
private static int[] toArray(String number) {
int[] array = new int[number.length()];
for (int i = 0; i < number.length(); i++) {
array[i] = Integer.parseInt(number.substring(i, i + 1));
}
return array;
}
/**
* 得到中文金额的整数部分。
*/
private static String getChineseInteger(int[] integers, boolean isMust5) {
StringBuffer chineseInteger = new StringBuffer("");
int length = integers.length;
for (int i = 0; i < length; i++) {
// 0出现在关键位置:1234(万)5678(亿)9012(万)3456(元)
// 特殊情况:10(拾元、壹拾元、壹拾万元、拾万元)
String key = "";
if (integers[i] == 0) {
if ((length - i) == 13)// 万(亿)(必填)
key = IUNIT[4];
else if ((length - i) == 9)// 亿(必填)
key = IUNIT[8];
else if ((length - i) == 5 && isMust5)// 万(不必填)
key = IUNIT[4];
else if ((length - i) == 1)// 元(必填)
key = IUNIT[0];
// 0遇非0时补零,不包含最后一位
if ((length - i) > 1 && integers[i + 1] != 0)
key += NUMBERS[0];
}
chineseInteger.append(integers[i] == 0 ? key : (NUMBERS[integers[i]] + IUNIT[length - i - 1]));
}
return chineseInteger.toString();
}
/**
* 得到中文金额的小数部分。TODO
*/
private static String getChineseDecimal(int[] decimals) {
StringBuffer chineseDecimal = new StringBuffer("");
for (int i = 0; i < decimals.length; i++) {
// 舍去3位小数之后的
if (i == 3)
break;
if (i == 0 && decimals[i] == 0) {
chineseDecimal.append("零");
}
chineseDecimal.append(decimals[i] == 0 ? "" : (NUMBERS[decimals[i]] + DUNIT[i]));
}
System.out.println(chineseDecimal.length());
if (chineseDecimal.length() < 2) {
return "整";
} else {
return chineseDecimal.toString();
}
}
/**
* 判断第5位数字的单位"万"是否应加。
*/
private static boolean isMust5(String integerStr) {
int length = integerStr.length();
if (length > 4) {
String subInteger = "";
if (length > 8) {
// 取得从低位数,第5到第8位的字串
subInteger = integerStr.substring(length - 8, length - 4);
} else {
subInteger = integerStr.substring(0, length - 4);
}
return Integer.parseInt(subInteger) > 0;
} else {
return false;
}
}
public synchronized static String toChineseNum(BigDecimal strNum) {
String fnum = FormatCurrency.getNumNoDot(strNum);
char[] cnum = fnum.toCharArray();
StringBuffer sb = new StringBuffer();
for (int i = 0; i < cnum.length; i++) {
sb.append(NUMBERS[Integer.valueOf(String.valueOf(cnum[i]))]);
}
return sb.toString();
}
public synchronized static String toChineseNum(String strNum) {
String fnum = FormatCurrency.getNumNoDot(Double.valueOf(strNum));
char[] cnum = fnum.toCharArray();
StringBuffer sb = new StringBuffer();
for (int i = 0; i < cnum.length; i++) {
sb.append(NUMBERS[Integer.valueOf(String.valueOf(cnum[i]))]);
}
return sb.toString();
}
public synchronized static String toChineseNum(double strNum) {
String fnum = FormatCurrency.getNumNoDot(strNum);
char[] cnum = fnum.toCharArray();
StringBuffer sb = new StringBuffer();
for (int i = 0; i < cnum.length; i++) {
sb.append(NUMBERS[Integer.valueOf(String.valueOf(cnum[i]))]);
}
return sb.toString();
}
public static void main(String[] args) {
String number = "606060.06";
System.out.println(number);
// System.out.println(toChinese(new BigDecimal(number)));
System.out.println(toChinese(number));
}
}
日期字符串转中文大写
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class DateToUpperChinese {
private static final String[] NUMBERS = { " 零 " , " 壹 " , " 贰 " , " 叁 " , " 肆 " , " 伍 " ,
" 陆 " , " 柒 " , " 捌 " , " 玖 " };
/** 通过 yyyy-MM-dd 得到中文大写格式 yyyy MM dd 日期 */
public static synchronized String toChinese(String str) {
StringBuffer sb = new StringBuffer();
sb.append(getSplitDateStr(str, 0 )).append( " " ).append(
getSplitDateStr(str, 1 )).append( " " ).append(
getSplitDateStr(str, 2 ));
return sb.toString();
}
/** 分别得到年月日的大写 默认分割符 "-" */
public static String getSplitDateStr(String str, int unit) {
// unit是单位 0=年 1=月 2日
String[] DateStr = str.split( " - " );
if (unit > DateStr.length)
unit = 0 ;
StringBuffer sb = new StringBuffer();
for ( int i = 0 ; i < DateStr[unit].length(); i ++ ) {
if ((unit == 1 || unit == 2 ) && Integer.valueOf(DateStr[unit]) > 9 ) {
sb.append(convertNum(DateStr[unit].substring( 0 , 1 )))
.append( " 拾 " ).append(
convertNum(DateStr[unit].substring( 1 , 2 )));
break ;
} else {
sb.append(convertNum(DateStr[unit].substring(i, i + 1 )));
}
}
if (unit == 1 || unit == 2 ) {
return sb.toString().replaceAll( " ^壹 " , "" ).replace( " 零 " , "" );
}
return sb.toString();
}
/** 转换数字为大写 */
private static String convertNum(String str) {
return NUMBERS[Integer.valueOf(str)];
}
/** 判断是否是零或正整数 */
public static boolean isNumeric(String str) {
Pattern pattern = Pattern.compile( " [0-9]* " );
Matcher isNum = pattern.matcher(str);
if ( ! isNum.matches()) {
return false ;
}
return true ;
}
public static void main(String args[]) {
System.out.println(toChinese( " 2008-10-02 " ));
}
}
货币 和 日期 转 中文大写相关推荐
- 货币数字转换成中文大写的函数
这个函数与以前其他网友提供的结构大不相同, 是一个从高位到低位的算法, 更加符合语言习惯. //---------------------------------------------------- ...
- C#中把货币、日期转换成中文大写
日期转换代码如下: /**//// <summary> /// 日期转换为中文大写 /// </summary> public class UpperConvert { ...
- JavaScript票据出票日期中文大写填写规范
说明: 本程序用JavaScript语言编写.转换方法遵照中国人民银行关于银行支票中票据出票日期的中文大写填写规范的相关规定. 中国人民银行规定的银行支票中关于中文大写填写规范的相关规定: 1.财政部 ...
- POI设置EXCEL单元格格式为文本、小数、百分比、货币、日期、科学计数法和中文大写...
再读本篇文章之前,请先看我的前一篇文章,前一篇文章中有重点讲到POI设置EXCEL单元格格式为文本格式,剩下的设置小数.百分比.货币.日期.科学计数法和中文大写这些将在下面一一写出 以下将要介绍的每一 ...
- Excel 货币中文大写汉字转化
Excel转化货币数字为中文大写:例如: 效果图: 代码: =IF(D8<1,IF(D8<0.1,TEXT(INT(D8*100),"[DBNum2]G/通用格式")& ...
- 阿拉伯数字转中文大写,以及票据日期的写法
最近闲来无事吧旧代码整理了一下.以前也看到有人写过类似的内容,觉得挺有意思,遂自己试着写了一下,拿来玩玩还是很有意思的.小数点后的就没有弄了. 参考的是银行的那些要求,但是有些规则其实没有完全细致约定 ...
- c#版的阿拉伯数字转中文大写,以及票据日期的写法
前接上篇,阿拉伯数字转中文数字是蛮有意思的,最近又有新发现~现在更新一下以前的代码~ c#版的阿拉伯数字转中文大写,以及票据日期的写法 不废话,直接上代码,两个方法 阿拉伯数字转中文: private ...
- 文件(夹)批量重命名数字、字母、日期、中文数字大写小写
首先,需要用到的这个工具: 百度 密码:qwu2 蓝奏云 密码:2r1z 目标是重命名下面5个文件(也可以是文件夹等,任意),从大写中文数字"贰"开始 打开工具,找到"文 ...
- (转)金额转中文大写
public class RMB {//返回转换好的大写形式public static String numberToRMB(String money) {return cleanZero(split ...
最新文章
- linux内核中的每cpu变量
- VTK:图像拉普拉斯算子用法实战
- “真三”中的人生哲理
- STS插件_ springsource-tool-suite插件各个历史版本
- 终极解密输入网址按回车到底发生了什么
- 【产品】产品经理常用的五大分析法
- 【VTK】VTK 之一 环境准备
- Android自定义ListView示例,以创建不可滚动的ListView
- android中的oom,Android OOM Adjustments
- swiftyjson_是时候放弃SwiftyJSON了
- 2020年全球石英晶振行业现状、竞争格局及未来发展趋势分析,5G推动万物互联,带动行业需求「图」
- js原生touch事件实现微信语音按住录音,上滑取消。
- gitee推送更新失败问题记录:remote: error: hook declined to update refs/heads/master
- 浅析阿里云API网关的产品架构和常见应用场景
- Win 10-jdk 8 下载和安装步骤
- Windows句柄数限制
- springboot实现几种常见登录(注册)方式
- cesium turf.js展示两多边形相交区域
- css之-各种基本图形画法总结(css3.0)
- TensorFlow与Flask结合打造手写体数字识别
热门文章
- 选择低功耗蓝牙芯片时应注意什么
- html阅读器怎么关闭,HTML – 屏幕阅读器如何处理显示flex?
- 程序猿二三事之工具使用-Eclipse调试Java常用功能介绍
- 【中级软考—软件设计师】2操作系统2.9 文件管理【***】:2.9.4 索引文件
- android仿win8 metro磁贴布局
- 走进微信公众号实现关注之后推送一条服务器自定义的消息给用户
- python编辑器哪款最好_python编辑器哪一款是比较好用的
- Selenium实现全自动打字
- 圣诞节要不要用Java发个邮件
- CentOs 6 杂文3!