Java实现数字⼤写转换
需求如下:⽤json读取后台⼯时信息,⽐如23.5⼩时,需要通过编码将其转换为贰拾叁点伍
⽐如23.23之前有对Stringl类型强转为Double在转为整型,发⽣了精度丢失,后来想想对⼩数点进⾏分割是个办法编码实现如下:
package day1;
import java.util.ArrayList;
import java.util.Collections;
import Pattern;
public class test_5 {
/**
* 单位进位,中⽂默认为4位即(万、亿)
*/
public static int UNIT_STEP = 4;
/**
* 单位
*/
public static String[] CN_UNITS = new String[] { "个", "⼗", "百", "千", "万", "⼗",
"百", "千", "亿", "⼗", "百", "千", "万" };
/**
* 汉字
*/
public static String[] CN_CHARS = new String[] { "零", "⼀", "⼆", "三", "四",
"五", "六", "七", "⼋", "九" };
/
**
* 将阿拉伯数字转换为中⽂数字123=》⼀⼆三
*
* @param srcNum
* @return
*/
public static String getCNNum(int srcNum) {
String desCNNum = "";
if (srcNum <= 0)
desCNNum = "零";
else {
int singleDigit;
while (srcNum > 0) {
singleDigit = srcNum % 10;
desCNNum = String.valueOf(CN_CHARS[singleDigit]) + desCNNum;
srcNum /= 10;
}
}
return desCNNum;
}
/**
* 数值转换为中⽂字符串如2转化为贰
*/
public String cvt(long num) {
return this.cvt(num, false);
}
/**
* 数值转换为中⽂字符串(⼝语化)
*
* @param num
*            需要转换的数值
* @param isColloquial
*            是否⼝语化。例如12转换为'⼗⼆'⽽不是'⼀⼗⼆'。
* @return
*/
public  String cvt(String num, boolean isColloquial) {
int integer, decimal;
StringBuffer strs = new StringBuffer(32);
String[] splitNum = num.split("\\.");
integer = Integer.parseInt(splitNum[0]);    //整数部分
decimal = Integer.parseInt(splitNum[1]);    //⼩数部分
String[] result_1 =convert(integer, isColloquial);
for (String str1 : result_1)
strs.append(str1);
if(decimal==0){//⼩数部分为0时
String();
}else{
String result_2 =getCNNum(decimal);  //例如5.32,⼩数部分展⽰三⼆,⽽不是三⼗⼆
strs.append("点");
strs.append(result_2);
String();
}
}
/*
* 对于int,long类型的数据处理
*/
public String cvt(long num, boolean isColloquial) {
String[] result = vert(num, isColloquial);
StringBuffer strs = new StringBuffer(32);
for (String str : result) {
strs.append(str);
}
String();
}
/**
* 将数值转换为中⽂
*
* @param num
*            需要转换的数值
* @param isColloquial
*            是否⼝语化。例如12转换为'⼗⼆'⽽不是'⼀⼗⼆'。
* @return
*/
public  String[] convert(long num, boolean isColloquial) {
if (num < 10) {// 10以下直接返回对应汉字
return new String[] { CN_CHARS[(int) num] };// ASCII2int
}
char[] chars = String.valueOf(num).toCharArray();
if (chars.length > CN_UNITS.length) {// 超过单位表⽰范围的返回空
return new String[] {};
}
boolean isLastUnitStep = false;// 记录上次单位进位
ArrayList<String> cnchars = new ArrayList<String>(chars.length * 2);// 创建数组,将数字填⼊单位对应的位置for (int pos = chars.length - 1; pos >= 0; pos--) {// 从低位向⾼位循环
char ch = chars[pos];
String cnChar = CN_CHARS[ch - '0'];// ascii2int 汉字
int unitPos = chars.length - pos - 1;// 对应的单位坐标
String cnUnit = CN_UNITS[unitPos];// 单位
boolean isZero = (ch == '0');// 是否为0
boolean isZeroLow = (pos + 1 < chars.length && chars[pos + 1] == '0');// 是否低位为0
boolean isUnitStep = (unitPos >= UNIT_STEP && (unitPos % UNIT_STEP == 0));// 当前位是否需要单位进位if (isUnitStep && isLastUnitStep) {// 去除相邻的上⼀个单位进位
int size = cnchars.size();
if (!CN_CHARS[0].(size - 2))) {// 补0
cnchars.add(CN_CHARS[0]);
}
}
if (isUnitStep || !isZero) {// 单位进位(万、亿),或者⾮0时加上单位
cnchars.add(cnUnit);
isLastUnitStep = isUnitStep;
}
if (isZero && (isZeroLow || isUnitStep)) {// 当前位为0低位为0,或者当前位为0并且为单位进位时进⾏省略
continue;
}
cnchars.add(cnChar);
isLastUnitStep = false;
}
// 清除最后⼀位的0
int chSize = cnchars.size();
String chEnd = (chSize - 1);
if (CN_CHARS[0].equals(chEnd) || CN_UNITS[0].equals(chEnd)) {
}
// ⼝语化处理
if (isColloquial) {
String chFirst = (0);
String chSecond = (1);
if (chFirst.equals(CN_CHARS[1]) && chSecond.startsWith(CN_UNITS[1])) {// 是否以'⼀'开头,紧跟'⼗'
}
}
Array(new String[] {});
}
public static void main(String[] args) {
java valueof
test_5 temp = new test_5();
String i ="40.3";
int t = 2234;
double j = 221.23;
System.out.println(temp.cvt(i,true));//四⼗点三
System.out.println(temp.cvt(t,true));//⼆千⼆百三⼗四
System.out.println(temp.cvt(""+j,true));//⼆百⼆⼗⼀点⼆三    }
}

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。