package com.zt.life.modules.mainPart.utils;
|
|
import java.util.regex.Matcher;
|
import java.util.regex.Pattern;
|
|
public class DigitChineseUtils {
|
static char[] cnArr = new char [] {'一','二','三','四','五','六','七','八','九'};
|
static char[] chArr = new char [] {'十','百','千','万','亿'};
|
static String allChineseNum = "零一二三四五六七八九十百千万亿";
|
|
|
static void compare(char s1[], char s2[]) {
|
for (int i = 0; i < s1.length; i++) {
|
int count = i + 1;
|
if (s1[i] != s2[i]) {
|
int ascll = s2[i];
|
{
|
//字符直接输出,防止输出ASCLL,数字进行转换,防止乱码;
|
if (ascll < 10) {
|
System.out.println("第" + count + "个位置的值不同,值为" + ascll);
|
} else
|
System.out.println("第" + count + "个位置的值不同,值为" + s2[i]);
|
}
|
|
}
|
}
|
|
}
|
|
/**
|
* 将汉字中的数字转换为阿拉伯数字
|
* @param chineseNum
|
* @return
|
*/
|
public static int chineseNumToArabicNum(String chineseNum) {
|
int result = 0;
|
int temp = 1;//存放一个单位的数字如:十万
|
int count = 0;//判断是否有chArr
|
for (int i = 0; i < chineseNum.length(); i++) {
|
boolean b = true;//判断是否是chArr
|
char c = chineseNum.charAt(i);
|
for (int j = 0; j < cnArr.length; j++) {//非单位,即数字
|
if (c == cnArr[j]) {
|
if(0 != count){//添加下一个单位之前,先把上一个单位值添加到结果中
|
result += temp;
|
temp = 1;
|
count = 0;
|
}
|
// 下标+1,就是对应的值
|
temp = j + 1;
|
b = false;
|
break;
|
}
|
}
|
if(b){//单位{'十','百','千','万','亿'}
|
for (int j = 0; j < chArr.length; j++) {
|
if (c == chArr[j]) {
|
switch (j) {
|
case 0:
|
temp *= 10;
|
break;
|
case 1:
|
temp *= 100;
|
break;
|
case 2:
|
temp *= 1000;
|
break;
|
case 3:
|
temp *= 10000;
|
break;
|
case 4:
|
temp *= 100000000;
|
break;
|
default:
|
break;
|
}
|
count++;
|
}
|
}
|
}
|
if (i == chineseNum.length() - 1) {//遍历到最后一个字符
|
result += temp;
|
}
|
}
|
return result;
|
}
|
|
/**
|
* 将数字转换为中文数字, 这里只写到了万
|
* @param intInput
|
* @return
|
*/
|
public static String arabicNumToChineseNum(int intInput) {
|
String si = String.valueOf(intInput);
|
String sd = "";
|
if (si.length() == 1) {
|
if (intInput == 0) {
|
return sd;
|
}
|
sd += cnArr[intInput - 1];
|
return sd;
|
} else if (si.length() == 2) {
|
if (si.substring(0, 1).equals("1")) {
|
sd += "十";
|
if (intInput % 10 == 0) {
|
return sd;
|
}
|
}
|
else
|
sd += (cnArr[intInput / 10 - 1] + "十");
|
sd += arabicNumToChineseNum(intInput % 10);
|
} else if (si.length() == 3) {
|
sd += (cnArr[intInput / 100 - 1] + "百");
|
if (String.valueOf(intInput % 100).length() < 2) {
|
if (intInput % 100 == 0) {
|
return sd;
|
}
|
sd += "零";
|
}
|
sd += arabicNumToChineseNum(intInput % 100);
|
} else if (si.length() == 4) {
|
sd += (cnArr[intInput / 1000 - 1] + "千");
|
if (String.valueOf(intInput % 1000).length() < 3) {
|
if (intInput % 1000 == 0) {
|
return sd;
|
}
|
sd += "零";
|
}
|
sd += arabicNumToChineseNum(intInput % 1000);
|
} else if (si.length() == 5) {
|
sd += (cnArr[intInput / 10000 - 1] + "万");
|
if (String.valueOf(intInput % 10000).length() < 4) {
|
if (intInput % 10000 == 0) {
|
return sd;
|
}
|
sd += "零";
|
}
|
sd += arabicNumToChineseNum(intInput % 10000);
|
}
|
|
return sd;
|
}
|
|
/**
|
* 是否为中文
|
* @param str
|
* @return
|
*/
|
public static boolean isChinese(String str) {
|
Pattern p = Pattern.compile("[\u4e00-\u9fa5]");
|
Matcher m = p.matcher(str);
|
if (m.find()) {
|
return true;
|
}
|
return false;
|
}
|
|
/**
|
* 数字 =>中文数字
|
*
|
* @param number
|
* @return
|
*/
|
public static String convert(int number) {
|
//数字对应的汉字
|
String[] num = {"零", "一", "二", "三", "四", "五", "六", "七", "八", "九"};
|
//单位
|
String[] unit = {"", "十", "百", "千", "万", "十", "百", "千", "亿", "十", "百", "千", "万亿"};
|
//将输入数字转换为字符串
|
String result = String.valueOf(number);
|
//将该字符串分割为数组存放
|
char[] ch = result.toCharArray();
|
//结果 字符串
|
String str = "";
|
int length = ch.length;
|
for (int i = 0; i < length; i++) {
|
int c = (int) ch[i] - 48;
|
if (c != 0) {
|
str += num[c] + unit[length - i - 1];
|
} else {
|
str += num[c];
|
}
|
}
|
return str;
|
}
|
|
/**
|
* 中文數字转阿拉伯数组【十万九千零六十 --> 109060】
|
* @param chineseNumber
|
* @return
|
*/
|
@SuppressWarnings("unused")
|
public static int chineseNumberInt(String chineseNumber){
|
int result = 0;
|
int temp = 1;//存放一个单位的数字如:十万
|
int count = 0;//判断是否有chArr
|
char[] cnArr = new char[]{'一','二','三','四','五','六','七','八','九'};
|
char[] chArr = new char[]{'十','百','千','万','亿'};
|
for (int i = 0; i < chineseNumber.length(); i++) {
|
boolean b = true;//判断是否是chArr
|
char c = chineseNumber.charAt(i);
|
for (int j = 0; j < cnArr.length; j++) {//非单位,即数字
|
if (c == cnArr[j]) {
|
if(0 != count){//添加下一个单位之前,先把上一个单位值添加到结果中
|
result += temp;
|
temp = 1;
|
count = 0;
|
}
|
// 下标+1,就是对应的值
|
temp = j + 1;
|
b = false;
|
break;
|
}
|
}
|
if(b){//单位{'十','百','千','万','亿'}
|
for (int j = 0; j < chArr.length; j++) {
|
if (c == chArr[j]) {
|
switch (j) {
|
case 0:
|
temp *= 10;
|
break;
|
case 1:
|
temp *= 100;
|
break;
|
case 2:
|
temp *= 1000;
|
break;
|
case 3:
|
temp *= 10000;
|
break;
|
case 4:
|
temp *= 100000000;
|
break;
|
default:
|
break;
|
}
|
count++;
|
}
|
}
|
}
|
if (i == chineseNumber.length() - 1) {//遍历到最后一个字符
|
result += temp;
|
}
|
}
|
return result;
|
}
|
|
}
|