金额转换,阿拉伯数字转换成中国传统形式。 写个测试方法!!急!!只要打印那一部分! 20
publicclassNumberConvertor{staticfinalcharNUMBER_CHAR[]=newchar[]{'零','壹','贰','叁','肆'...
public class NumberConvertor {
static final char NUMBER_CHAR[] = new char[] { '零', '壹', '贰', '叁', '肆',
'伍', '陆', '柒', '捌', '玖' };
static final char UNIT_CHAR[] = new char[] { '拾', '佰', '仟', '万', '拾', '佰',
'仟', '亿', '拾', '佰', '仟' };
public String convert(int number) {
int remainingNumber = number;
StringBuffer sb = new StringBuffer();
int pos = 0;
int preDigit = -1;
while (remainingNumber > 0) {
int digit = remainingNumber % 10;
if (isUnitToPrint(digit, preDigit, pos)) {
sb.insert(0, UNIT_CHAR[pos - 1]);
}
if (isDigitToPrint(digit, preDigit, pos))
sb.insert(0, NUMBER_CHAR[digit]);
pos++;
preDigit = digit;
remainingNumber = remainingNumber / 10;
}
return sb.toString();
}
private boolean isDigitToPrint(int digit, int preDigit, int pos) {
//个,万,亿的零不输出
if (digit == 0 && (pos == 0 || pos == 4 || pos == 8)) {
return false;
}
//前后相邻,高位零不输出
if (digit == 0 && preDigit == 0) {
return false;
}
return true;
}
private boolean isUnitToPrint(int digit, int preDigit, int pos) {
//个位没有单位
if (pos == 0) {
return false;
}
//万,亿必须输出
if (pos == 5 || pos == 9) {
return true;
}
//零后不输出单位
if (digit == 0) {
return false;
}
return true;
}
} 展开
static final char NUMBER_CHAR[] = new char[] { '零', '壹', '贰', '叁', '肆',
'伍', '陆', '柒', '捌', '玖' };
static final char UNIT_CHAR[] = new char[] { '拾', '佰', '仟', '万', '拾', '佰',
'仟', '亿', '拾', '佰', '仟' };
public String convert(int number) {
int remainingNumber = number;
StringBuffer sb = new StringBuffer();
int pos = 0;
int preDigit = -1;
while (remainingNumber > 0) {
int digit = remainingNumber % 10;
if (isUnitToPrint(digit, preDigit, pos)) {
sb.insert(0, UNIT_CHAR[pos - 1]);
}
if (isDigitToPrint(digit, preDigit, pos))
sb.insert(0, NUMBER_CHAR[digit]);
pos++;
preDigit = digit;
remainingNumber = remainingNumber / 10;
}
return sb.toString();
}
private boolean isDigitToPrint(int digit, int preDigit, int pos) {
//个,万,亿的零不输出
if (digit == 0 && (pos == 0 || pos == 4 || pos == 8)) {
return false;
}
//前后相邻,高位零不输出
if (digit == 0 && preDigit == 0) {
return false;
}
return true;
}
private boolean isUnitToPrint(int digit, int preDigit, int pos) {
//个位没有单位
if (pos == 0) {
return false;
}
//万,亿必须输出
if (pos == 5 || pos == 9) {
return true;
}
//零后不输出单位
if (digit == 0) {
return false;
}
return true;
}
} 展开
2个回答
展开全部
public class NumberConvertor {
static final char NUMBER_CHAR[] = new char[] { '零', '壹', '贰', '叁', '肆',
'伍', '陆', '柒', '捌', '玖' };
static final char UNIT_CHAR[] = new char[] { '拾', '佰', '仟', '万', '拾', '佰',
'仟', '亿', '拾', '佰', '仟' };
public String convert(int number) {
int remainingNumber = number;
StringBuffer sb = new StringBuffer();
int pos = 0;
int preDigit = -1;
while (remainingNumber > 0) {
int digit = remainingNumber % 10;
if (isUnitToPrint(digit, preDigit, pos)) {
sb.insert(0, UNIT_CHAR[pos - 1]);
}
if (isDigitToPrint(digit, preDigit, pos))
sb.insert(0, NUMBER_CHAR[digit]);
pos++;
preDigit = digit;
remainingNumber = remainingNumber / 10;
}
return sb.toString();
}
private boolean isDigitToPrint(int digit, int preDigit, int pos) {
//个,万,亿的零不输出
if (digit == 0 && (pos == 0 || pos == 4 || pos == 8)) {
return false;
}
//前后相邻,高位零不输出
if (digit == 0 && preDigit == 0) {
return false;
}
return true;
}
private boolean isUnitToPrint(int digit, int preDigit, int pos) {
//个位没有单位
if (pos == 0) {
return false;
}
//万,亿必须输出
if (pos == 5 || pos == 9) {
return true;
}
//零后不输出单位
if (digit == 0) {
return false;
}
return true;
}
static final char NUMBER_CHAR[] = new char[] { '零', '壹', '贰', '叁', '肆',
'伍', '陆', '柒', '捌', '玖' };
static final char UNIT_CHAR[] = new char[] { '拾', '佰', '仟', '万', '拾', '佰',
'仟', '亿', '拾', '佰', '仟' };
public String convert(int number) {
int remainingNumber = number;
StringBuffer sb = new StringBuffer();
int pos = 0;
int preDigit = -1;
while (remainingNumber > 0) {
int digit = remainingNumber % 10;
if (isUnitToPrint(digit, preDigit, pos)) {
sb.insert(0, UNIT_CHAR[pos - 1]);
}
if (isDigitToPrint(digit, preDigit, pos))
sb.insert(0, NUMBER_CHAR[digit]);
pos++;
preDigit = digit;
remainingNumber = remainingNumber / 10;
}
return sb.toString();
}
private boolean isDigitToPrint(int digit, int preDigit, int pos) {
//个,万,亿的零不输出
if (digit == 0 && (pos == 0 || pos == 4 || pos == 8)) {
return false;
}
//前后相邻,高位零不输出
if (digit == 0 && preDigit == 0) {
return false;
}
return true;
}
private boolean isUnitToPrint(int digit, int preDigit, int pos) {
//个位没有单位
if (pos == 0) {
return false;
}
//万,亿必须输出
if (pos == 5 || pos == 9) {
return true;
}
//零后不输出单位
if (digit == 0) {
return false;
}
return true;
}
追问
看好题目!
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
package com.txx.test;
public class Test5 {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
NumberConvertor s = new NumberConvertor();
System.out.println(s.convert(1234));
}
}
public class Test5 {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
NumberConvertor s = new NumberConvertor();
System.out.println(s.convert(1234));
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询