java将阿拉伯数字转换为中文
来源:互联网 发布:最简单的c语言小游戏 编辑:程序博客网 时间:2024/06/05 00:35
package cn.com.goldpalm;
/**
* @authorwusong
*
*/
public class CnConversion2{
//阿拉伯数字,字符串类型
public String number;
//阿拉伯数字,int类型
public int inumber;
//阿拉伯数字的位数
public int size;
//构造函数
CnConversion2(String value){
number = value;
inumber = Integer.parseInt(value);
size = CnConversion2.getLength(inumber);
}
//阿拉伯数字每一位
public static final String[] num = {"0","1","2","3","4","5","6","7","8","9"};
//中文数字每一位
public static final String[] china = {"零","一","二","三","四","五","六","七","八","九"};
//中文数字进位度量
public static final String[] degree = {"十","百","千","万","亿"};
//计算输入数据长度
public static int getLength(int value){
return (int)Math.floor((int)(Math.log10(value)+1));
}
//将输入数字转换成相应文字,不添加度量
public static String convertAtoW(String value){
for(int i=0;i<num.length;i++){
value = value.replaceAll(num[i],china[i]);
}
return value;
}
/**度量处理方法,处理到最高位为九位的数字,如果处理更高位的数字则需要做微小改动
* @param value处理的数字
* @return
*/
public String degree(String value){
StringBuffer temp = new StringBuffer(value);
//添加"拾"
if(size>1){
temp = temp.insert(temp.length()-1,degree[0]);
}
//添加"佰"
if(size>2){
temp = temp.insert(temp.length()-3,degree[1]);
}
//添加"仟"
if(size>3){
temp = temp.insert(temp.length()-5,degree[2]);
}
//添加"万"
if(size>4){
temp = temp.insert(temp.length()-7,degree[3]);
}
//添加"拾万"
if(size>5){
temp = temp.insert(temp.length()-9,degree[0]);
}
//添加"佰万"
if(size>6){
temp = temp.insert(temp.length()-11,degree[1]);
}
//添加"仟万"
if(size>7){
temp = temp.insert(temp.length()-13,degree[2]);
}
//添加"亿"
if(size>8){
temp = temp.insert(temp.length()-15,degree[4]);
}
//添加"拾亿"以后处理类似,需要用BigInteger
return temp.toString();
}
//处理多零情况
public String degreezero(String value){
String temp = new String(value);
while((temp.indexOf("零千")!=-1)||(temp.indexOf("零百")!=-1)||(temp.indexOf("零十")!=-1)||(temp.indexOf("零零")!=-1)){
if(temp.indexOf("零千")!=-1){
temp = temp.replaceAll("零千","零");
}
if(temp.indexOf("零百")!=-1){
temp = temp.replaceAll("零百","零");
}
if(temp.indexOf("零十")!=-1){
temp = temp.replaceAll("零十","零");
}
if(temp.indexOf("零零")!=-1){
temp = temp.replaceAll("零零","零");
}
}
if((temp.indexOf("零万")!=-1)){
temp = temp.replaceAll("零万","万");
}
if((temp.indexOf("亿万")!=-1)){
temp = temp.replaceAll("万","");
}
//末尾不能有零
if(temp.lastIndexOf("零")==temp.length()-1){
temp = temp.substring(0,temp.length()-1);
}
return temp;
}
public static void main(String[] args) {
// TODO Auto-generated method stub
CnConversion2 google = new CnConversion2("20000000");
String result = google.degreezero(google.degree(CnConversion2.convertAtoW(google.number)));
System.out.println(result);
}
}
/**
* @authorwusong
*
*/
public class CnConversion2{
//阿拉伯数字,字符串类型
public String number;
//阿拉伯数字,int类型
public int inumber;
//阿拉伯数字的位数
public int size;
//构造函数
CnConversion2(String value){
number = value;
inumber = Integer.parseInt(value);
size = CnConversion2.getLength(inumber);
}
//阿拉伯数字每一位
public static final String[] num = {"0","1","2","3","4","5","6","7","8","9"};
//中文数字每一位
public static final String[] china = {"零","一","二","三","四","五","六","七","八","九"};
//中文数字进位度量
public static final String[] degree = {"十","百","千","万","亿"};
//计算输入数据长度
public static int getLength(int value){
return (int)Math.floor((int)(Math.log10(value)+1));
}
//将输入数字转换成相应文字,不添加度量
public static String convertAtoW(String value){
for(int i=0;i<num.length;i++){
value = value.replaceAll(num[i],china[i]);
}
return value;
}
/**度量处理方法,处理到最高位为九位的数字,如果处理更高位的数字则需要做微小改动
* @param value处理的数字
* @return
*/
public String degree(String value){
StringBuffer temp = new StringBuffer(value);
//添加"拾"
if(size>1){
temp = temp.insert(temp.length()-1,degree[0]);
}
//添加"佰"
if(size>2){
temp = temp.insert(temp.length()-3,degree[1]);
}
//添加"仟"
if(size>3){
temp = temp.insert(temp.length()-5,degree[2]);
}
//添加"万"
if(size>4){
temp = temp.insert(temp.length()-7,degree[3]);
}
//添加"拾万"
if(size>5){
temp = temp.insert(temp.length()-9,degree[0]);
}
//添加"佰万"
if(size>6){
temp = temp.insert(temp.length()-11,degree[1]);
}
//添加"仟万"
if(size>7){
temp = temp.insert(temp.length()-13,degree[2]);
}
//添加"亿"
if(size>8){
temp = temp.insert(temp.length()-15,degree[4]);
}
//添加"拾亿"以后处理类似,需要用BigInteger
return temp.toString();
}
//处理多零情况
public String degreezero(String value){
String temp = new String(value);
while((temp.indexOf("零千")!=-1)||(temp.indexOf("零百")!=-1)||(temp.indexOf("零十")!=-1)||(temp.indexOf("零零")!=-1)){
if(temp.indexOf("零千")!=-1){
temp = temp.replaceAll("零千","零");
}
if(temp.indexOf("零百")!=-1){
temp = temp.replaceAll("零百","零");
}
if(temp.indexOf("零十")!=-1){
temp = temp.replaceAll("零十","零");
}
if(temp.indexOf("零零")!=-1){
temp = temp.replaceAll("零零","零");
}
}
if((temp.indexOf("零万")!=-1)){
temp = temp.replaceAll("零万","万");
}
if((temp.indexOf("亿万")!=-1)){
temp = temp.replaceAll("万","");
}
//末尾不能有零
if(temp.lastIndexOf("零")==temp.length()-1){
temp = temp.substring(0,temp.length()-1);
}
return temp;
}
public static void main(String[] args) {
// TODO Auto-generated method stub
CnConversion2 google = new CnConversion2("20000000");
String result = google.degreezero(google.degree(CnConversion2.convertAtoW(google.number)));
System.out.println(result);
}
}
0 0
- java将阿拉伯数字转换为中文
- java将阿拉伯数字转换为中文数字
- JAVA将阿拉伯数字转换为中文
- java将阿拉伯数字转换为中文数字
- 将阿拉伯数字123转换为中文大写
- 将阿拉伯数字转换为中文数字
- IOS,将阿拉伯数字转换为中文数字
- java将中文数字替换为阿拉伯数字
- java转换中文数字为阿拉伯数字
- 【iOS开发系列】将阿拉伯数字转换为中文数字
- delphi下将中文数字转换为阿拉伯数字
- 将阿拉伯数字转换为罗马数字
- 将阿拉伯数字转换为罗马数字
- 阿拉伯数字转换为中文汉字
- 阿拉伯数字转换为中文数字
- 阿拉伯数字转换为中文数字
- 用java代码将阿拉伯数字金额转换成中文大写
- java笔试题之----将阿拉伯数字转换成中文
- java 语音
- MySQL单列索引和组合索引的区别介绍
- 可以自动登录的Ubuntu,重置root和其他普通用户密码的方法
- 苹果iOS 7后台机制解析
- 二叉树的遍历(递归,非递归,层次)
- java将阿拉伯数字转换为中文
- Newtonsoft.Json取json字符串中的值得用法
- jQuery获取页面及元素高度、宽度
- Ubuntu 14.04中文输入法的安装
- c# 解析JSON的几种办法
- (译)KVO的内部实现
- acess trunk hybrid 二三层转发原理
- php获取客户端IP地址的几种方法(转)
- oracle pl/sql异常机制 模拟抛出异常