Java number to Chinese capital currency
Publish: 2021-04-23 05:46:58
1. 没有小数点吧首先做个2映射 1对一,2对二等等还有一个1对十,2对百,等等等这个映射,其实就是Map的Key和value的关系如果是整数的话,循环对10取余,1234%10=4,然后根据第一个映射,4映射四放到字符串里,做个标记变量,记录循环次数,1对十,2对百,再加到字符串里 代码如下import java.util.HashMap;
import java.util.Map;public class Tsss {
public static void main(String[] args) {
int m=2354745;
int i=0;
Tsss t=new Tsss();
String s="";
while(m!=0){
if(i!=0){
s=map1.get(i)+s;
}
int n=m%10;
System.out.println(n);
s=map.get(n)+s;
m=m/10;
i++;
}
System.out.println(s);
}
public static Map<Integer,String> map=new HashMap<Integer,String>();
{
map.put(1, "一");
map.put(2, "二");
map.put(3, "三");
map.put(4, "四");
map.put(5, "五");
map.put(6, "六");
map.put(7, "七");
map.put(8, "八");
map.put(9, "九");
map.put(0, "十");
}
public static Map<Integer,String> map1=new HashMap<Integer,String>();
{
map1.put(1, "十");
map1.put(2, "百");
map1.put(3, "千");
map1.put(4, "万");
map1.put(5, "十万");
map1.put(6, "百万");
map1.put(7, "千万");
map1.put(8, "亿");
map1.put(9, "十亿");
map1.put(0, "百亿");
}
}
import java.util.Map;public class Tsss {
public static void main(String[] args) {
int m=2354745;
int i=0;
Tsss t=new Tsss();
String s="";
while(m!=0){
if(i!=0){
s=map1.get(i)+s;
}
int n=m%10;
System.out.println(n);
s=map.get(n)+s;
m=m/10;
i++;
}
System.out.println(s);
}
public static Map<Integer,String> map=new HashMap<Integer,String>();
{
map.put(1, "一");
map.put(2, "二");
map.put(3, "三");
map.put(4, "四");
map.put(5, "五");
map.put(6, "六");
map.put(7, "七");
map.put(8, "八");
map.put(9, "九");
map.put(0, "十");
}
public static Map<Integer,String> map1=new HashMap<Integer,String>();
{
map1.put(1, "十");
map1.put(2, "百");
map1.put(3, "千");
map1.put(4, "万");
map1.put(5, "十万");
map1.put(6, "百万");
map1.put(7, "千万");
map1.put(8, "亿");
map1.put(9, "十亿");
map1.put(0, "百亿");
}
}
2. String words="12334";
for(int i=0 ; i<words.length ; i++)
{
switch(words.charAt(i))
{
case "1":System.out.println("壹");break;
case "2":
..............
case "0":System.out.println("零");break;
}
}
大致思路是这个样子的吧
for(int i=0 ; i<words.length ; i++)
{
switch(words.charAt(i))
{
case "1":System.out.println("壹");break;
case "2":
..............
case "0":System.out.println("零");break;
}
}
大致思路是这个样子的吧
3. eclipse中用java实现中文和阿拉伯数字互转的方法如下:
import java.io.*;
import java.lang.IllegalArgumentException;
public class ConvertNum{
/**
* 把金额阿拉伯数字转换为汉字表示,小数点后四舍五入保留两位
* 还有一种方法可以在转换的过程中不考虑连续0的情况,然后对最终的结果进行一次遍历合并连续的零
*/
public static String [] ChineseNum = new String[]{"零","壹","贰","叁","肆","伍","陆","柒","捌","玖"};
public static String NumToChinese(double num){
if(num > 99999999999999.99 || num < -99999999999999.99)
throw new IllegalArgumentException("参数值超出允许范围 (-99999999999999.99 ~ 99999999999999.99)");
boolean negative = false;//正负标号
if(num<0){
negative = true;
num = num*(-1);
}
long temp = Math.round(num*100);
int numFen=(int)(temp%10);//分
temp=temp/10;
int numJiao = (int)(temp%10);//角
temp=temp/10;
//此时temp只包含整数部分
int [] parts =new int[20];//将金额整数部分分为在0-9999之间数的各个部分
int numParts = 0;//记录把原来金额整数部分分割为几个部分
for(int i=0;;i++){
if(temp == 0)
break;
int part = (int)(temp%10000);
parts[i] =part;
temp = temp/10000;
numParts++;
}
boolean beforeWanIsZero = true;//标志位,记录万的下一级是否为0
String chineseStr = "";
for(int i=0;i<numParts;i++){
String partChinese = partConvert(parts[i]);
if(i%2==0){
if("".equals(partChinese))
beforeWanIsZero = true;
else
beforeWanIsZero = false;
}
if(i!=0){
if(i%2==0)//亿的部分
chineseStr = "亿"+chineseStr;
else{
if("".equals(partChinese)&&!beforeWanIsZero)// 如果“万”对应的 part 为 0,而“万”下面一级不为 0,则不加“万”,而加“零”
chineseStr = "零"+chineseStr;
else{
if(parts[i-1]<1000&&parts[i-1]>0)//如果万的部分不为0,而万前面的部分小于1000大于0,则万后面应该跟零
chineseStr = "零"+chineseStr;
chineseStr = "万"+chineseStr;
}
}
}
chineseStr = partChinese + chineseStr;
}
if("".equals(chineseStr))//整数部分为0,则表示为零元
chineseStr = ChineseNum[0];
else if(negative)//整数部分部位0,但是为负数
chineseStr = "负" +chineseStr;
chineseStr = chineseStr + "元";
if(numFen==0&&numJiao==0){
chineseStr = chineseStr +"整";
}
else if(numFen==0){//0分
chineseStr = chineseStr +ChineseNum[numJiao] + "角";
}
else{
if(numJiao==0)
chineseStr = chineseStr + "零" + ChineseNum[numFen] + "分";
else
chineseStr = chineseStr + ChineseNum[numJiao] + "角" + ChineseNum[numFen] + "分";
}
return chineseStr;
}
//转换拆分后的每个部分,0-9999之间
public static String partConvert(int partNum){
if(partNum<0||partNum>10000){
throw new IllegalArgumentException("参数必须是大于等于0或小于10000的整数");
}
String [] units = new String[]{"","拾","佰","仟"};
int temp = partNum;
String partResult = new Integer(partNum).toString();
int partResultLength = partResult.length();
boolean lastIsZero = true;//记录上一位是否为0
String chineseStr = "";
for(int i=0;i<partResultLength;i++){
if(temp == 0)//高位无数字
break;
int digit = temp%10;
if(digit == 0){
if(!lastIsZero)//如果前一个数字不是0则在当前汉字串前加零
chineseStr = "零"+chineseStr;
lastIsZero = true;
}
else{
chineseStr = ChineseNum[digit] + units[i] +chineseStr;
lastIsZero = false;
}
temp =temp/10;
}
return chineseStr;
}
public static void main(String args []){
double num = 0;
System.out.println("请输入金额数字,-1退出");
try{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
num = Double.parseDouble(br.readLine());
}catch(IOException e){
System.out.println(e.toString());
}
while(num!=-1){
System.out.println(num+NumToChinese(num));
try{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
num = Double.parseDouble(br.readLine());
}catch(IOException e){
System.out.println(e.toString());
}
}
System.out.println("其他测试:");
System.out.println("100120: " + NumToChinese(100120));
System.out.println("25000000000005.999: " + NumToChinese(25000000000005.999));
System.out.println("45689263.626: " + NumToChinese(45689263.626));
System.out.println("0.69457: " + NumToChinese(0.69457));
System.out.println("253.0: " + NumToChinese(253.0));
System.out.println("0: " + NumToChinese(0));
}
}
import java.io.*;
import java.lang.IllegalArgumentException;
public class ConvertNum{
/**
* 把金额阿拉伯数字转换为汉字表示,小数点后四舍五入保留两位
* 还有一种方法可以在转换的过程中不考虑连续0的情况,然后对最终的结果进行一次遍历合并连续的零
*/
public static String [] ChineseNum = new String[]{"零","壹","贰","叁","肆","伍","陆","柒","捌","玖"};
public static String NumToChinese(double num){
if(num > 99999999999999.99 || num < -99999999999999.99)
throw new IllegalArgumentException("参数值超出允许范围 (-99999999999999.99 ~ 99999999999999.99)");
boolean negative = false;//正负标号
if(num<0){
negative = true;
num = num*(-1);
}
long temp = Math.round(num*100);
int numFen=(int)(temp%10);//分
temp=temp/10;
int numJiao = (int)(temp%10);//角
temp=temp/10;
//此时temp只包含整数部分
int [] parts =new int[20];//将金额整数部分分为在0-9999之间数的各个部分
int numParts = 0;//记录把原来金额整数部分分割为几个部分
for(int i=0;;i++){
if(temp == 0)
break;
int part = (int)(temp%10000);
parts[i] =part;
temp = temp/10000;
numParts++;
}
boolean beforeWanIsZero = true;//标志位,记录万的下一级是否为0
String chineseStr = "";
for(int i=0;i<numParts;i++){
String partChinese = partConvert(parts[i]);
if(i%2==0){
if("".equals(partChinese))
beforeWanIsZero = true;
else
beforeWanIsZero = false;
}
if(i!=0){
if(i%2==0)//亿的部分
chineseStr = "亿"+chineseStr;
else{
if("".equals(partChinese)&&!beforeWanIsZero)// 如果“万”对应的 part 为 0,而“万”下面一级不为 0,则不加“万”,而加“零”
chineseStr = "零"+chineseStr;
else{
if(parts[i-1]<1000&&parts[i-1]>0)//如果万的部分不为0,而万前面的部分小于1000大于0,则万后面应该跟零
chineseStr = "零"+chineseStr;
chineseStr = "万"+chineseStr;
}
}
}
chineseStr = partChinese + chineseStr;
}
if("".equals(chineseStr))//整数部分为0,则表示为零元
chineseStr = ChineseNum[0];
else if(negative)//整数部分部位0,但是为负数
chineseStr = "负" +chineseStr;
chineseStr = chineseStr + "元";
if(numFen==0&&numJiao==0){
chineseStr = chineseStr +"整";
}
else if(numFen==0){//0分
chineseStr = chineseStr +ChineseNum[numJiao] + "角";
}
else{
if(numJiao==0)
chineseStr = chineseStr + "零" + ChineseNum[numFen] + "分";
else
chineseStr = chineseStr + ChineseNum[numJiao] + "角" + ChineseNum[numFen] + "分";
}
return chineseStr;
}
//转换拆分后的每个部分,0-9999之间
public static String partConvert(int partNum){
if(partNum<0||partNum>10000){
throw new IllegalArgumentException("参数必须是大于等于0或小于10000的整数");
}
String [] units = new String[]{"","拾","佰","仟"};
int temp = partNum;
String partResult = new Integer(partNum).toString();
int partResultLength = partResult.length();
boolean lastIsZero = true;//记录上一位是否为0
String chineseStr = "";
for(int i=0;i<partResultLength;i++){
if(temp == 0)//高位无数字
break;
int digit = temp%10;
if(digit == 0){
if(!lastIsZero)//如果前一个数字不是0则在当前汉字串前加零
chineseStr = "零"+chineseStr;
lastIsZero = true;
}
else{
chineseStr = ChineseNum[digit] + units[i] +chineseStr;
lastIsZero = false;
}
temp =temp/10;
}
return chineseStr;
}
public static void main(String args []){
double num = 0;
System.out.println("请输入金额数字,-1退出");
try{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
num = Double.parseDouble(br.readLine());
}catch(IOException e){
System.out.println(e.toString());
}
while(num!=-1){
System.out.println(num+NumToChinese(num));
try{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
num = Double.parseDouble(br.readLine());
}catch(IOException e){
System.out.println(e.toString());
}
}
System.out.println("其他测试:");
System.out.println("100120: " + NumToChinese(100120));
System.out.println("25000000000005.999: " + NumToChinese(25000000000005.999));
System.out.println("45689263.626: " + NumToChinese(45689263.626));
System.out.println("0.69457: " + NumToChinese(0.69457));
System.out.println("253.0: " + NumToChinese(253.0));
System.out.println("0: " + NumToChinese(0));
}
}
4. public class RenMingBi {
/**
* @param args add by zxx ,Nov 29, 2008
*/
private static final char[] data = new char[]{
'零','壹','贰','叁','肆','伍','陆','柒','捌','玖'
};
private static final char[] units = new char[]{
'元','拾','佰','仟','万','拾','佰','仟','亿'
};
public static void main(String[] args) {
// TODO Auto-generated method stub
System.out.println(
convert(135689123));
}
public static String convert(int money)
{
StringBuffer sbf = new StringBuffer();
int unit = 0;
while(money!=0)
{
sbf.insert(0,units[unit++]);
int number = money%10;
sbf.insert(0, data[number]);
money /= 10;
}
return sbf.toString();
}
}
/**
* @param args add by zxx ,Nov 29, 2008
*/
private static final char[] data = new char[]{
'零','壹','贰','叁','肆','伍','陆','柒','捌','玖'
};
private static final char[] units = new char[]{
'元','拾','佰','仟','万','拾','佰','仟','亿'
};
public static void main(String[] args) {
// TODO Auto-generated method stub
System.out.println(
convert(135689123));
}
public static String convert(int money)
{
StringBuffer sbf = new StringBuffer();
int unit = 0;
while(money!=0)
{
sbf.insert(0,units[unit++]);
int number = money%10;
sbf.insert(0, data[number]);
money /= 10;
}
return sbf.toString();
}
}
5. public class Admin {
public static void main(String[] args) {
String[] arr = { " 零", & " 壹", & " 贰", & " 叁", & " 肆 " 伍 " 陆", & " 柒 " 捌", & " 玖" };< br />
String str = " 123456"< br />
char[] c = str.toCharArray();< br /> for (int i = 0; i < c.length; i++) {
int a = Integer.parseInt(String.valueOf(c[i]));< br /> System.out.print(arr[a]);< br /> }
}
}
public static void main(String[] args) {
String[] arr = { " 零", & " 壹", & " 贰", & " 叁", & " 肆 " 伍 " 陆", & " 柒 " 捌", & " 玖" };< br />
String str = " 123456"< br />
char[] c = str.toCharArray();< br /> for (int i = 0; i < c.length; i++) {
int a = Integer.parseInt(String.valueOf(c[i]));< br /> System.out.print(arr[a]);< br /> }
}
}
6. package com.heyang;
/**
* 将10亿以内的阿拉伯数字转成汉字大写形式
* @author xizhenyin
*
*/
public class CnUpperCaser {
// 整数部分
private String integerPart;
// 小数部分
private String floatPart;
// 将数字转化为汉字的数组,因为各个实例都要使用所以设为静态
private static final char[] cnNumbers={'零','壹','贰','叁','肆','伍','陆','柒','捌','玖'};
// 供分级转化的数组,因为各个实例都要使用所以设为静态
private static final char[] series={'元','拾','百','仟','万','拾','百','仟','亿'};
/**
* 构造函数,通过它将阿拉伯数字形式的字符串传入
* @param original
*/
public CnUpperCaser(String original){
// 成员变量初始化
integerPart="";
floatPart="";
if(original.contains(".")){
// 如果包含小数点
int dotIndex=original.indexOf(".");
integerPart=original.substring(0,dotIndex);
floatPart=original.substring(dotIndex+1);
}
else{
// 不包含小数点
integerPart=original;
}
}
/**
* 取得大写形式的字符串
* @return
*/
public String getCnString(){
// 因为是累加所以用StringBuffer
StringBuffer sb=new StringBuffer();
// 整数部分处理
for(int i=0;i<integerPart.length();i++){
int number=getNumber(integerPart.charAt(i));
sb.append(cnNumbers[number]);
sb.append(series[integerPart.length()-1-i]);
}
// 小数部分处理
if(floatPart.length()>0){
sb.append("点");
for(int i=0;i<floatPart.length();i++){
int number=getNumber(floatPart.charAt(i));
sb.append(cnNumbers[number]);
}
}
// 返回拼接好的字符串
return sb.toString();
}
/**
* 将字符形式的数字转化为整形数字
* 因为所有实例都要用到所以用静态修饰
* @param c
* @return
*/
private static int getNumber(char c){
String str=String.valueOf(c);
return Integer.parseInt(str);
}
/**
* @param args
*/
public static void main(String[] args) {
System.out.println(new CnUpperCaser("123456789.12345").getCnString());
System.out.println(new CnUpperCaser("123456789").getCnString());
System.out.println(new CnUpperCaser(".123456789").getCnString());
System.out.println(new CnUpperCaser("0.1234").getCnString());
System.out.println(new CnUpperCaser("1").getCnString());
System.out.println(new CnUpperCaser("12").getCnString());
System.out.println(new CnUpperCaser("123").getCnString());
System.out.println(new CnUpperCaser("1234").getCnString());
System.out.println(new CnUpperCaser("12345").getCnString());
System.out.println(new CnUpperCaser("123456").getCnString());
System.out.println(new CnUpperCaser("1234567").getCnString());
System.out.println(new CnUpperCaser("12345678").getCnString());
System.out.println(new CnUpperCaser("123456789").getCnString());
}
}
/**
* 将10亿以内的阿拉伯数字转成汉字大写形式
* @author xizhenyin
*
*/
public class CnUpperCaser {
// 整数部分
private String integerPart;
// 小数部分
private String floatPart;
// 将数字转化为汉字的数组,因为各个实例都要使用所以设为静态
private static final char[] cnNumbers={'零','壹','贰','叁','肆','伍','陆','柒','捌','玖'};
// 供分级转化的数组,因为各个实例都要使用所以设为静态
private static final char[] series={'元','拾','百','仟','万','拾','百','仟','亿'};
/**
* 构造函数,通过它将阿拉伯数字形式的字符串传入
* @param original
*/
public CnUpperCaser(String original){
// 成员变量初始化
integerPart="";
floatPart="";
if(original.contains(".")){
// 如果包含小数点
int dotIndex=original.indexOf(".");
integerPart=original.substring(0,dotIndex);
floatPart=original.substring(dotIndex+1);
}
else{
// 不包含小数点
integerPart=original;
}
}
/**
* 取得大写形式的字符串
* @return
*/
public String getCnString(){
// 因为是累加所以用StringBuffer
StringBuffer sb=new StringBuffer();
// 整数部分处理
for(int i=0;i<integerPart.length();i++){
int number=getNumber(integerPart.charAt(i));
sb.append(cnNumbers[number]);
sb.append(series[integerPart.length()-1-i]);
}
// 小数部分处理
if(floatPart.length()>0){
sb.append("点");
for(int i=0;i<floatPart.length();i++){
int number=getNumber(floatPart.charAt(i));
sb.append(cnNumbers[number]);
}
}
// 返回拼接好的字符串
return sb.toString();
}
/**
* 将字符形式的数字转化为整形数字
* 因为所有实例都要用到所以用静态修饰
* @param c
* @return
*/
private static int getNumber(char c){
String str=String.valueOf(c);
return Integer.parseInt(str);
}
/**
* @param args
*/
public static void main(String[] args) {
System.out.println(new CnUpperCaser("123456789.12345").getCnString());
System.out.println(new CnUpperCaser("123456789").getCnString());
System.out.println(new CnUpperCaser(".123456789").getCnString());
System.out.println(new CnUpperCaser("0.1234").getCnString());
System.out.println(new CnUpperCaser("1").getCnString());
System.out.println(new CnUpperCaser("12").getCnString());
System.out.println(new CnUpperCaser("123").getCnString());
System.out.println(new CnUpperCaser("1234").getCnString());
System.out.println(new CnUpperCaser("12345").getCnString());
System.out.println(new CnUpperCaser("123456").getCnString());
System.out.println(new CnUpperCaser("1234567").getCnString());
System.out.println(new CnUpperCaser("12345678").getCnString());
System.out.println(new CnUpperCaser("123456789").getCnString());
}
}
7. /**
* 金额小数转换成中文大写金额
* @author Neil Han
*
*/
public class ConvertMoneyToUppercase {
private static final String UNIT[] = { "万", "千", "佰", "拾", "亿", "千", "佰",
"拾", "万", "千", "佰", "拾", "元", "角", "分" };
private static final String NUM[] = { "零", "壹", "贰", "叁", "肆", "伍", "陆",
"柒", "捌", "玖" };
private static final double MAX_VALUE = 9999999999999.99D;
/**
* 将金额小数转换成中文大写金额
* @param money
* @return result
*/
public static String convertMoney(double money) {
if (money < 0 || money > MAX_VALUE)
return "参数非法!";
long money1 = Math.round(money * 100); // 四舍五入到分
if (money1 == 0)
return "零元整";
String strMoney = String.valueOf(money1);
int numIndex = 0; // numIndex用于选择金额数值
int unitIndex = UNIT.length - strMoney.length(); // unitIndex用于选择金额单位
boolean isZero = false; // 用于判断当前为是否为零
String result = "";
for (; numIndex < strMoney.length(); numIndex++, unitIndex++) {
char num = strMoney.charAt(numIndex);
if (num == '0') {
isZero = true;
if (UNIT[unitIndex] == "亿" || UNIT[unitIndex] == "万"
|| UNIT[unitIndex] == "元") { // 如果当前位是亿、万、元,且数值为零
result = result + UNIT[unitIndex]; //补单位亿、万、元
isZero = false;
}
}else {
if (isZero) {
result = result + "零";
isZero = false;
}
result = result + NUM[Integer.parseInt(String.valueOf(num))] + UNIT[unitIndex];
}
}
//不是角分结尾就加"整"字
if (!result.endsWith("角")&&!result.endsWith("分")) {
result = result + "整";
}
//例如没有这行代码,数值"400000001101.2",输出就是"肆千亿万壹千壹佰零壹元贰角"
result = result.replaceAll("亿万", "亿");
return result;
}
public static void main(String[] args) {
double value = Double.parseDouble("40330701101.2");
System.out.println("您输入的金额小写为:" + value);
System.out.println("您输入的金额大写为:" + convertMoney(value));
}
}
* 金额小数转换成中文大写金额
* @author Neil Han
*
*/
public class ConvertMoneyToUppercase {
private static final String UNIT[] = { "万", "千", "佰", "拾", "亿", "千", "佰",
"拾", "万", "千", "佰", "拾", "元", "角", "分" };
private static final String NUM[] = { "零", "壹", "贰", "叁", "肆", "伍", "陆",
"柒", "捌", "玖" };
private static final double MAX_VALUE = 9999999999999.99D;
/**
* 将金额小数转换成中文大写金额
* @param money
* @return result
*/
public static String convertMoney(double money) {
if (money < 0 || money > MAX_VALUE)
return "参数非法!";
long money1 = Math.round(money * 100); // 四舍五入到分
if (money1 == 0)
return "零元整";
String strMoney = String.valueOf(money1);
int numIndex = 0; // numIndex用于选择金额数值
int unitIndex = UNIT.length - strMoney.length(); // unitIndex用于选择金额单位
boolean isZero = false; // 用于判断当前为是否为零
String result = "";
for (; numIndex < strMoney.length(); numIndex++, unitIndex++) {
char num = strMoney.charAt(numIndex);
if (num == '0') {
isZero = true;
if (UNIT[unitIndex] == "亿" || UNIT[unitIndex] == "万"
|| UNIT[unitIndex] == "元") { // 如果当前位是亿、万、元,且数值为零
result = result + UNIT[unitIndex]; //补单位亿、万、元
isZero = false;
}
}else {
if (isZero) {
result = result + "零";
isZero = false;
}
result = result + NUM[Integer.parseInt(String.valueOf(num))] + UNIT[unitIndex];
}
}
//不是角分结尾就加"整"字
if (!result.endsWith("角")&&!result.endsWith("分")) {
result = result + "整";
}
//例如没有这行代码,数值"400000001101.2",输出就是"肆千亿万壹千壹佰零壹元贰角"
result = result.replaceAll("亿万", "亿");
return result;
}
public static void main(String[] args) {
double value = Double.parseDouble("40330701101.2");
System.out.println("您输入的金额小写为:" + value);
System.out.println("您输入的金额大写为:" + convertMoney(value));
}
}
8. Let's take a good look at the problems with the array subscript. It's better to use debug
step by step to see which step is the problem
I also have the ready-made code. If it doesn't work, I can give you one
hope to adopt.
step by step to see which step is the problem
I also have the ready-made code. If it doesn't work, I can give you one
hope to adopt.
9. Public class text {
/ *
* test program feasibility
* @ param args
* /
public static void main (string [] args) {
system. Out. Println & quote-------- Convert the number into Chinese amount in words; n");< br />
Trans2RMB t2r = new Trans2RMB();< br />
String s = t2r.cleanZero(t2r.splitNum(t2r.roundString(t2r.getNum())));
/ / if there is an empty string after conversion, the screen will not be output
if (& quot;& quot;. equals(s)) {
System.out.println(" After being translated into Chinese, it is: & quot+ s);;< br />
}
System.out.println(" 92; n---------------------------------------------");
}
/**
* receives a number from the command line, in which checkNum () method is called
* verification, and the corresponding value
* is returned. If the input is valid, it returns the number of input data, which is returned to the data. br />
System.out.println(" Please enter a number (accurate to two decimal places): & quot
/ / enter the floating-point number from the command line
java.util.scanner scanner = new java.util.scanner (system. In)< br />
s = scanner.next();
/ / judge whether the user input is legal
/ / if it is legal, return this value; If illegal return & quot; 0"< br />
if(this.checkNum(s)) {
return s;< br />
} else {
return "& quot;;
}
}
/ *
* judge whether the data entered by the user is legal or not. The user can only enter numbers greater than zero, but not other characters
* @ params string
* @ return. If the data entered by the user is legal, return true, Otherwise, return false
* /
private Boolean checknum (string s) {
/ / if the number entered by the user contains non numeric characters, it will be regarded as illegal data, and return false
try {
float f = float. Valueof (s)
/ / if the number is less than zero, it will be regarded as illegal data and return false
if (F & lt; 0) {
System.out.println(" Illegal data, please check& quot;);< br />
return false;< br />
}else {
return true;< br />
}
} catch (NumberFormatException e) {
System.out.println(" Illegal data, please check& quot;);< br />
return false;
}
}
/ *
* divides the number entered by the user into decimal points, And call numformat() method
* to convert the corresponding Chinese amount in capital form
* Note: the number passed in should be the Chinese amount in capital form string converted by roundstring() method
* @ params string
* @ return
< private string splitnum (string) s) {
/ / if an empty string is passed in, continue to return the empty string
If & quot& quot;. equals(s)) {
return "& quot;;
}
/ / splits the string with a decimal point
int index = s.indexof & quot& quot;);
/ / intercepts and converts the integer part of the number
string into only = S. substring (0, index)< br />
String part1 = this.numFormat(1, intOnly);
/ / intercepts and converts the decimal part of the number
string smallonly = s.substring (index + 1)< br />
String part2 = this.numFormat(2, smallOnly);
/ / reassemble the converted integer part and decimal part into a new string
string news = Part1 + part2< br />
return newS;<
}
/ *
* rounds the incoming number
* @ params string the number entered from the command line
* @ return the new value after rounding
* /
private string roundstring (string s) {
/ / if the incoming string is empty, continue to return the empty string
if & quote& quot;. equals(s)) {
return "& quot;;
}
/ / convert the number to double type and round it
double D = double. Parsedouble (s)
/ / this operation acts on two decimal places
d = (d * 100 + 0.5) / 100
/ / format d
s = new Java. Text. Decimalformat & quot## 0.000"). format(d);
/ / splits the string with a decimal point
int index = s.indexof & quot& quot;);
/ / the integer part of this number
string into only = S. substring (0, index)
/ / the maximum length of the specified value can only be up to trillions of units, otherwise & quot; 0"< br />
if(intOnly.length()13) {
System.out.println(" Input data too large Integer part up to 13 bits!)& quot;);< br />
return "& quot;;
}
/ / the decimal part of the number
string smallonly = S. substring (index + 1)
/ / if the decimal part is greater than two digits, only two digits after the decimal point are truncated
if (smallonly. Length() 2) {
string roundsmall = smallonly. Substring (0, 2)
/ / reassemble the integer part and the newly truncated decimal part of the string
s = into only + & quot& quot; + roundSmall;< br />
}
return s;
}
/ *
* converts the incoming amount into Chinese amount in capital form
* @ param flag int flag bit, 1 represents the integer part of the conversion, 0 means to convert the decimal part
* @ param s string to be converted
* @ return converted Chinese amount with unit in capital form
* /
private string numformat (int flag, string s) {
int slength = s.length()
/ / currency capitalization
string bigletter args) {
system. Out. Println & quot-------- Convert the number into Chinese amount in words; n");< br />
Trans2RMB t2r = new Trans2RMB();< br />
String s = t2r.cleanZero(t2r.splitNum(t2r.roundString(t2r.getNum())));
/ / if there is an empty string after conversion, the screen will not be output
if (& quot;& quot;. equals(s)) {
System.out.println(" After being translated into Chinese, it is: & quot+ s);;< br />
}
System.out.println(" 92; n---------------------------------------------");
}
/**
* receives a number from the command line, in which checkNum () method is called
* verification, and the corresponding value
* is returned. If the input is valid, it returns the number of input data, which is returned to the data. br />
System.out.println(" Please enter a number (accurate to two decimal places): & quot
/ / enter the floating-point number from the command line
java.util.scanner scanner = new java.util.scanner (system. In)< br />
s = scanner.next();
/ / judge whether the user input is legal
/ / if it is legal, return this value; If illegal return & quot; 0"< br />
if(this.checkNum(s)) {
return s;< br />
} else {
return "& quot;;
}
}
/ *
* judge whether the data entered by the user is legal or not. The user can only enter numbers greater than zero, but not other characters
* @ params string
* @ return. If the data entered by the user is legal, return true, Otherwise, return false
* /
private Boolean checknum (string s) {
/ / if the number entered by the user contains non numeric characters, it will be regarded as illegal data, and return false
try {
float f = float. Valueof (s)
/ / if the number is less than zero, it will be regarded as illegal data and return false
if (F & lt; 0) {
System.out.println(" Illegal data, please check& quot;);< br />
return false;< br />
}else {
return true;< br />
}
} catch (NumberFormatException e) {
System.out.println(" Illegal data, please check& quot;);< br />
return false;
}
}
/ *
* divides the number entered by the user into decimal points, And call numformat() method
* to convert the corresponding Chinese amount in capital form
* Note: the number passed in should be the Chinese amount in capital form string converted by roundstring() method
* @ params string
* @ return
< private string splitnum (string) s) {
/ / if an empty string is passed in, continue to return the empty string
If & quot& quot;. equals(s)) {
return "& quot;;
}
/ / splits the string with a decimal point
int index = s.indexof & quot& quot;);
/ / intercepts and converts the integer part of the number
string into only = S. substring (0, index)< br />
String part1 = this.numFormat(1, intOnly);
/ / intercepts and converts the decimal part of the number
string smallonly = s.substring (index + 1)< br />
String part2 = this.numFormat(2, smallOnly);
/ / reassemble the converted integer part and decimal part into a new string
string news = Part1 + part2< br />
return newS;<
}
/ *
* rounds the incoming number
* @ params string the number entered from the command line
* @ return the new value after rounding
* /
private string roundstring (string s) {
/ / if the incoming string is empty, continue to return the empty string
if & quote& quot;. equals(s)) {
return "& quot;;
}
/ / convert the number to double type and round it
double D = double. Parsedouble (s)
/ / this operation acts on two decimal places
d = (d * 100 + 0.5) / 100
/ / format d
s = new Java. Text. Decimalformat & quot## 0.000"). format(d);
/ / splits the string with a decimal point
int index = s.indexof & quot& quot;);
/ / the integer part of this number
string into only = S. substring (0, index)
/ / the maximum length of the specified value can only be up to trillions of units, otherwise & quot; 0"< br />
if(intOnly.length()13) {
System.out.println(" Input data too large Integer part up to 13 bits!)& quot;);< br />
/ *
* test program feasibility
* @ param args
* /
public static void main (string [] args) {
system. Out. Println & quote-------- Convert the number into Chinese amount in words; n");< br />
Trans2RMB t2r = new Trans2RMB();< br />
String s = t2r.cleanZero(t2r.splitNum(t2r.roundString(t2r.getNum())));
/ / if there is an empty string after conversion, the screen will not be output
if (& quot;& quot;. equals(s)) {
System.out.println(" After being translated into Chinese, it is: & quot+ s);;< br />
}
System.out.println(" 92; n---------------------------------------------");
}
/**
* receives a number from the command line, in which checkNum () method is called
* verification, and the corresponding value
* is returned. If the input is valid, it returns the number of input data, which is returned to the data. br />
System.out.println(" Please enter a number (accurate to two decimal places): & quot
/ / enter the floating-point number from the command line
java.util.scanner scanner = new java.util.scanner (system. In)< br />
s = scanner.next();
/ / judge whether the user input is legal
/ / if it is legal, return this value; If illegal return & quot; 0"< br />
if(this.checkNum(s)) {
return s;< br />
} else {
return "& quot;;
}
}
/ *
* judge whether the data entered by the user is legal or not. The user can only enter numbers greater than zero, but not other characters
* @ params string
* @ return. If the data entered by the user is legal, return true, Otherwise, return false
* /
private Boolean checknum (string s) {
/ / if the number entered by the user contains non numeric characters, it will be regarded as illegal data, and return false
try {
float f = float. Valueof (s)
/ / if the number is less than zero, it will be regarded as illegal data and return false
if (F & lt; 0) {
System.out.println(" Illegal data, please check& quot;);< br />
return false;< br />
}else {
return true;< br />
}
} catch (NumberFormatException e) {
System.out.println(" Illegal data, please check& quot;);< br />
return false;
}
}
/ *
* divides the number entered by the user into decimal points, And call numformat() method
* to convert the corresponding Chinese amount in capital form
* Note: the number passed in should be the Chinese amount in capital form string converted by roundstring() method
* @ params string
* @ return
< private string splitnum (string) s) {
/ / if an empty string is passed in, continue to return the empty string
If & quot& quot;. equals(s)) {
return "& quot;;
}
/ / splits the string with a decimal point
int index = s.indexof & quot& quot;);
/ / intercepts and converts the integer part of the number
string into only = S. substring (0, index)< br />
String part1 = this.numFormat(1, intOnly);
/ / intercepts and converts the decimal part of the number
string smallonly = s.substring (index + 1)< br />
String part2 = this.numFormat(2, smallOnly);
/ / reassemble the converted integer part and decimal part into a new string
string news = Part1 + part2< br />
return newS;<
}
/ *
* rounds the incoming number
* @ params string the number entered from the command line
* @ return the new value after rounding
* /
private string roundstring (string s) {
/ / if the incoming string is empty, continue to return the empty string
if & quote& quot;. equals(s)) {
return "& quot;;
}
/ / convert the number to double type and round it
double D = double. Parsedouble (s)
/ / this operation acts on two decimal places
d = (d * 100 + 0.5) / 100
/ / format d
s = new Java. Text. Decimalformat & quot## 0.000"). format(d);
/ / splits the string with a decimal point
int index = s.indexof & quot& quot;);
/ / the integer part of this number
string into only = S. substring (0, index)
/ / the maximum length of the specified value can only be up to trillions of units, otherwise & quot; 0"< br />
if(intOnly.length()13) {
System.out.println(" Input data too large Integer part up to 13 bits!)& quot;);< br />
return "& quot;;
}
/ / the decimal part of the number
string smallonly = S. substring (index + 1)
/ / if the decimal part is greater than two digits, only two digits after the decimal point are truncated
if (smallonly. Length() 2) {
string roundsmall = smallonly. Substring (0, 2)
/ / reassemble the integer part and the newly truncated decimal part of the string
s = into only + & quot& quot; + roundSmall;< br />
}
return s;
}
/ *
* converts the incoming amount into Chinese amount in capital form
* @ param flag int flag bit, 1 represents the integer part of the conversion, 0 means to convert the decimal part
* @ param s string to be converted
* @ return converted Chinese amount with unit in capital form
* /
private string numformat (int flag, string s) {
int slength = s.length()
/ / currency capitalization
string bigletter args) {
system. Out. Println & quot-------- Convert the number into Chinese amount in words; n");< br />
Trans2RMB t2r = new Trans2RMB();< br />
String s = t2r.cleanZero(t2r.splitNum(t2r.roundString(t2r.getNum())));
/ / if there is an empty string after conversion, the screen will not be output
if (& quot;& quot;. equals(s)) {
System.out.println(" After being translated into Chinese, it is: & quot+ s);;< br />
}
System.out.println(" 92; n---------------------------------------------");
}
/**
* receives a number from the command line, in which checkNum () method is called
* verification, and the corresponding value
* is returned. If the input is valid, it returns the number of input data, which is returned to the data. br />
System.out.println(" Please enter a number (accurate to two decimal places): & quot
/ / enter the floating-point number from the command line
java.util.scanner scanner = new java.util.scanner (system. In)< br />
s = scanner.next();
/ / judge whether the user input is legal
/ / if it is legal, return this value; If illegal return & quot; 0"< br />
if(this.checkNum(s)) {
return s;< br />
} else {
return "& quot;;
}
}
/ *
* judge whether the data entered by the user is legal or not. The user can only enter numbers greater than zero, but not other characters
* @ params string
* @ return. If the data entered by the user is legal, return true, Otherwise, return false
* /
private Boolean checknum (string s) {
/ / if the number entered by the user contains non numeric characters, it will be regarded as illegal data, and return false
try {
float f = float. Valueof (s)
/ / if the number is less than zero, it will be regarded as illegal data and return false
if (F & lt; 0) {
System.out.println(" Illegal data, please check& quot;);< br />
return false;< br />
}else {
return true;< br />
}
} catch (NumberFormatException e) {
System.out.println(" Illegal data, please check& quot;);< br />
return false;
}
}
/ *
* divides the number entered by the user into decimal points, And call numformat() method
* to convert the corresponding Chinese amount in capital form
* Note: the number passed in should be the Chinese amount in capital form string converted by roundstring() method
* @ params string
* @ return
< private string splitnum (string) s) {
/ / if an empty string is passed in, continue to return the empty string
If & quot& quot;. equals(s)) {
return "& quot;;
}
/ / splits the string with a decimal point
int index = s.indexof & quot& quot;);
/ / intercepts and converts the integer part of the number
string into only = S. substring (0, index)< br />
String part1 = this.numFormat(1, intOnly);
/ / intercepts and converts the decimal part of the number
string smallonly = s.substring (index + 1)< br />
String part2 = this.numFormat(2, smallOnly);
/ / reassemble the converted integer part and decimal part into a new string
string news = Part1 + part2< br />
return newS;<
}
/ *
* rounds the incoming number
* @ params string the number entered from the command line
* @ return the new value after rounding
* /
private string roundstring (string s) {
/ / if the incoming string is empty, continue to return the empty string
if & quote& quot;. equals(s)) {
return "& quot;;
}
/ / convert the number to double type and round it
double D = double. Parsedouble (s)
/ / this operation acts on two decimal places
d = (d * 100 + 0.5) / 100
/ / format d
s = new Java. Text. Decimalformat & quot## 0.000"). format(d);
/ / splits the string with a decimal point
int index = s.indexof & quot& quot;);
/ / the integer part of this number
string into only = S. substring (0, index)
/ / the maximum length of the specified value can only be up to trillions of units, otherwise & quot; 0"< br />
if(intOnly.length()13) {
System.out.println(" Input data too large Integer part up to 13 bits!)& quot;);< br />
10. /**
* 金额小数转换成中文大写金额
* @author Neil Han
*
*/
public class ConvertMoneyToUppercase {
private static final String UNIT[] = { "万", "千", "佰", "拾", "亿", "千", "佰",
"拾", "万", "千", "佰", "拾", "元", "角", "分" };
private static final String NUM[] = { "零", "壹", "贰", "叁", "肆", "伍", "陆",
"柒", "捌", "玖" };
private static final double MAX_VALUE = 9999999999999.99D;
/**
* 将金额小数转换成中文大写金额
* @param money
* @return result
*/
public static String convertMoney(double money) {
if (money < 0 || money > MAX_VALUE)
return "参数非法!";
long money1 = Math.round(money * 100); // 四舍五入到分
if (money1 == 0)
return "零元整";
String strMoney = String.valueOf(money1);
int numIndex = 0; // numIndex用于选择金额数值
int unitIndex = UNIT.length - strMoney.length(); // unitIndex用于选择金额单位
boolean isZero = false; // 用于判断当前为是否为零
String result = "";
for (; numIndex < strMoney.length(); numIndex++, unitIndex++) {
char num = strMoney.charAt(numIndex);
if (num == '0') {
isZero = true;
if (UNIT[unitIndex] == "亿" || UNIT[unitIndex] == "万"
|| UNIT[unitIndex] == "元") { // 如果当前位是亿、万、元,且数值为零
result = result + UNIT[unitIndex]; //补单位亿、万、元
isZero = false;
}
}else {
if (isZero) {
result = result + "零";
isZero = false;
}
result = result + NUM[Integer.parseInt(String.valueOf(num))] + UNIT[unitIndex];
}
}
//不是角分结尾就加"整"字
if (!result.endsWith("角")&&!result.endsWith("分")) {
result = result + "整";
}
//例如没有这行代码,数值"400000001101.2",输出就是"肆千亿万壹千壹佰零壹元贰角"
result = result.replaceAll("亿万", "亿");
return result;
}
public static void main(String[] args) {
double value = Double.parseDouble("40330701101.2");
System.out.println("您输入的金额小写为:" + value);
System.out.println("您输入的金额大写为:" + convertMoney(value));
}
}
* 金额小数转换成中文大写金额
* @author Neil Han
*
*/
public class ConvertMoneyToUppercase {
private static final String UNIT[] = { "万", "千", "佰", "拾", "亿", "千", "佰",
"拾", "万", "千", "佰", "拾", "元", "角", "分" };
private static final String NUM[] = { "零", "壹", "贰", "叁", "肆", "伍", "陆",
"柒", "捌", "玖" };
private static final double MAX_VALUE = 9999999999999.99D;
/**
* 将金额小数转换成中文大写金额
* @param money
* @return result
*/
public static String convertMoney(double money) {
if (money < 0 || money > MAX_VALUE)
return "参数非法!";
long money1 = Math.round(money * 100); // 四舍五入到分
if (money1 == 0)
return "零元整";
String strMoney = String.valueOf(money1);
int numIndex = 0; // numIndex用于选择金额数值
int unitIndex = UNIT.length - strMoney.length(); // unitIndex用于选择金额单位
boolean isZero = false; // 用于判断当前为是否为零
String result = "";
for (; numIndex < strMoney.length(); numIndex++, unitIndex++) {
char num = strMoney.charAt(numIndex);
if (num == '0') {
isZero = true;
if (UNIT[unitIndex] == "亿" || UNIT[unitIndex] == "万"
|| UNIT[unitIndex] == "元") { // 如果当前位是亿、万、元,且数值为零
result = result + UNIT[unitIndex]; //补单位亿、万、元
isZero = false;
}
}else {
if (isZero) {
result = result + "零";
isZero = false;
}
result = result + NUM[Integer.parseInt(String.valueOf(num))] + UNIT[unitIndex];
}
}
//不是角分结尾就加"整"字
if (!result.endsWith("角")&&!result.endsWith("分")) {
result = result + "整";
}
//例如没有这行代码,数值"400000001101.2",输出就是"肆千亿万壹千壹佰零壹元贰角"
result = result.replaceAll("亿万", "亿");
return result;
}
public static void main(String[] args) {
double value = Double.parseDouble("40330701101.2");
System.out.println("您输入的金额小写为:" + value);
System.out.println("您输入的金额大写为:" + convertMoney(value));
}
}
Hot content