货币 和 日期 转 中文大写

货币数值转中文大写

package com.th.kits.Impl;

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));
}

}

日期字符串转中文大写

package com.th.kits.Impl;

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

public class DateToUpperChinese{

private static final String[]NUMBERS = { " " , " " , " " , " " , " " , " " ,
" " , " " , " " , " " };

/** 通过yyyy-MM-dd得到中文大写格式yyyyMMdd日期 */
public static synchronized StringtoChinese(Stringstr){
StringBuffersb
= new StringBuffer();
sb.append(getSplitDateStr(str,
0 )).append( " " ).append(
getSplitDateStr(str,
1 )).append( " " ).append(
getSplitDateStr(str,
2 ));
return sb.toString();
}

/** 分别得到年月日的大写默认分割符"-" */
public static StringgetSplitDateStr(Stringstr, int unit){
// unit是单位0=年1=月2日
String[]DateStr = str.split( " - " );
if (unit > DateStr.length)
unit
= 0 ;
StringBuffersb
= 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 StringconvertNum(Stringstr){
return NUMBERS[Integer.valueOf(str)];
}

/** 判断是否是零或正整数 */
public static boolean isNumeric(Stringstr){
Patternpattern
= Pattern.compile( " [0-9]* " );
MatcherisNum
= pattern.matcher(str);
if ( ! isNum.matches()){
return false ;
}
return true ;
}

public static void main(Stringargs[]){

System.out.println(toChinese(
" 2008-10-02 " ));

}

}

你可能感兴趣的:(F#)