62,614
社区成员
发帖
与我相关
我的任务
分享
把
else{
min = getNum(ch);
if((min == 5 && max == 1)||(min == 9 && max < 9)){
max = min;
min = 1;
}
else if(max==9 && min==9){
max += 9;
min = 1;
}
}
改为
else{
min = getNum(ch);
if(min == 5)
max += 4;
else if(min == 9){
if((max/4)%2 == 0)
max +=8;
else
max +=4;
}
}
public class ChangeToNum {
private static String numStr = "一二三四五六七八九";
public ChangeToNum() {
System.out.println(change("十一亿零五百万零十八"));
}
public static String change(String old){
char[] newNum = new char[15];
int temp = 1,max = 1,min = 1;
old = old.replaceAll("零","");
old = old.replaceAll("百十","百一十");
old = old.replaceAll("千十","千一十");
old = old.replaceAll("万十","万一十");
old = old.replaceAll("亿十","亿一十");
if(old.charAt(0)=='十')
old = "一"+old;
for(int i = 0;i<newNum.length;i++){
newNum[i] = '0';
}
char ch;
for(int i = old.length()-1;i>=0;i--){
ch = old.charAt(i);
if(numStr.indexOf(ch) != -1){
temp = getNum(ch);
newNum[newNum.length - min-max+1] = (char)(temp +48);
}
else{
min = getNum(ch);
if((min == 5 && max == 1)||(min == 9 && max < 9)){
max = min;
min = 1;
}
else if(max==9 && min==9){
max += 9;
min = 1;
}
}
}
return (new String(newNum)).substring(newNum.length - min-max+1);
}
private static int getNum(char ch){
int index1 = numStr.indexOf(ch);
int index2 = "十百千万零零零亿".indexOf(ch);
if(index1 == -1 && index2 == -1){
System.out.println("Fatal Error.");
System.exit(0);
}
return index1 == -1?index2 + 2:index1 + 1;
}
public static void main(String[] args) {
new ChangeToNum();
}
}
把
if(r>max)
max *= r;
改为
if(r>max)
max = r;
应该也可以的。
public class ConvertNumber {
/** 定义数组存放数字对应的大写 */
private final static String[] STR_NUMBER = { "零", "壹", "贰", "叁", "肆", "伍", "陆", "柒", "捌", "玖" };
/** 定义数组存放位数的大写 */
private final static String[] STR_MODIFY = { "", "拾", "佰", "仟", "万", "拾", "佰", "仟", "亿", "拾", "佰", "仟" };
/**
* 转化整数部分
*
* @param tempString
* @return 返回整数部分
*/
private static String getInteger(String tempString) {
/** 用来保存整数部分数字串 */
String strInteger = null;//
/** 记录"."所在位置 */
int intDotPos = tempString.indexOf(".");
int intSignPos = tempString.indexOf("-");
if (intDotPos == -1)
intDotPos = tempString.length();
/** 取出整数部分 */
strInteger = tempString.substring(intSignPos + 1, intDotPos);
strInteger = new StringBuffer(strInteger).reverse().toString();
StringBuffer sbResult = new StringBuffer();
for (int i = 0; i < strInteger.length(); i++) {
sbResult.append(STR_MODIFY[i]);
sbResult.append(STR_NUMBER[strInteger.charAt(i) - 48]);
}
sbResult = sbResult.reverse();
replace(sbResult, "零拾", "零");
replace(sbResult, "零佰", "零");
replace(sbResult, "零仟", "零");
replace(sbResult, "零万", "万");
replace(sbResult, "零亿", "亿");
replace(sbResult, "零零", "零");
replace(sbResult, "零零零", "零");
/** 这两句不能颠倒顺序 */
replace(sbResult, "零零零零万", "");
replace(sbResult, "零零零零", "");
/** 这样读起来更习惯. */
replace(sbResult, "壹拾亿", "拾亿");
replace(sbResult, "壹拾万", "拾万");
/** 删除个位上的零 */
if (sbResult.charAt(sbResult.length() - 1) == '零' && sbResult.length() != 1)
sbResult.deleteCharAt(sbResult.length() - 1);
if (strInteger.length() == 2) {
replace(sbResult, "壹拾", "拾");
}
/** 将结果反转回来. */
return sbResult.toString();
}
/**
* 转化小数部分 例:输入22.34返回叁肆
*
* @param tempString
* @return
*/
private static String getFraction(String tempString) {
String strFraction = null;
int intDotPos = tempString.indexOf(".");
/** 没有点说明没有小数,直接返回 */
if (intDotPos == -1)
return "";
strFraction = tempString.substring(intDotPos + 1);
StringBuffer sbResult = new StringBuffer(strFraction.length());
for (int i = 0; i < strFraction.length(); i++) {
sbResult.append(STR_NUMBER[strFraction.charAt(i) - 48]);
}
return sbResult.toString();
}
/**
* 判断传入的字符串中是否有.如果有则返回点
*
* @param tempString
* @return
*/
private static String getDot(String tempString) {
return tempString.indexOf(".") != -1 ? "点" : "";
}
/**
* 判断传入的字符串中是否有-如果有则返回负
*
* @param tempString
* @return
*/
private static String getSign(String tempString) {
return tempString.indexOf("-") != -1 ? "负" : "";
}
/**
* 将一个数字转化为金额
*
* @param tempNumber 传入一个double的变量
* @return 返一个转换好的字符串
*/
public static String numberToChinese(double tempNumber) {
java.text.DecimalFormat df = new java.text.DecimalFormat("#.#########");
String pTemp = String.valueOf(df.format(tempNumber));
StringBuffer sbResult = new StringBuffer(getSign(pTemp) + getInteger(pTemp) + getDot(pTemp) + getFraction(pTemp));
return sbResult.toString();
}
public static String numberToChinese(BigDecimal tempNumber) {
return numberToChinese(tempNumber.doubleValue());
}
/**
* 替代字符
*
* @param pValue
* @param pSource
* @param pDest
*/
private static void replace(StringBuffer pValue, String pSource, String pDest) {
if (pValue == null || pSource == null || pDest == null)
return;
/** 记录pSource在pValue中的位置 */
int intPos = 0;
do {
intPos = pValue.toString().indexOf(pSource);
/** 没有找到pSource */
if (intPos == -1)
break;
pValue.delete(intPos, intPos + pSource.length());
pValue.insert(intPos, pDest);
} while (true);
}
/**
* @param args
*/
public static void main(String[] args) {
System.out.println(numberToChinese(1230400567.8934));
}
}
把
if(max>sign)
sign *= max;
改成
if(max>sign)
sign *= r;
就OK了。
package 把人民币转化为大写汉字;
public class number {
// 建立数组, 用来查找各级的标志, 这一级的值为Math.pow(10, 下标 + 1);
public static char[] spliter = { '十', '百', '千', '万' };
// 建立数组, 用来查找中文中的数字与何拉伯数字的对应关系,
// 下标值为相应阿拉伯数字的值
public static char[] number = { '零', '一', '二', '三', '四', '五', '六', '七',
'八', '九'};
// 存放中文的数值
private String data =null;
public number(String data) {
this.data = data;
}
/**
* 返回中文数字的阿拉伯数字的值.
*/
public int parse() {
int result = 0;
int index = 1; // 如果第一个字符是级别标志, 这时就用到了index=1这个值
for (int i = 0; i < data.length(); ++i) {
if (isNumber(data.charAt(i))) {
// 是数字, 则找到在建立的中文数字的数组中的下标,
// 即为他的数值
index = find(number, data.charAt(i));
} else {
// 读到一个级别标志时, 进行计算一次
int temp = find(spliter, data.charAt(i));
result = result + index * (int) Math.pow(10, temp + 1);
}
}
// 处理最后一个字符, 如果最后一个字符是数字, 则把加上他的值
// 因为前面是读取级别标志时才进行计算一次.
int last = data.length() - 1;
if (isNumber(data.charAt(last))) {
result += find(number, data.charAt(last));
}
return result;
}
// 查找一个字符在给定数组中的位置
public static int find(char[] array, char ch) {
for (int i = 0; i < array.length; ++i) {
if (array[i] == ch) {
return i;
}
}
return -1;
}
// 字符是否数字
public static boolean isNumber(char ch) {
for (int i = 0; i < number.length; ++i) {
if (ch == number[i]) {
return true;
}
}
return false;
}
// 字符是否级别的标志
public static boolean isSpliter(char ch) {
for (int i = 0; i < spliter.length; ++i) {
if (ch == spliter[i]) {
return true;
}
}
return false;
}
public static void main(String[] args) {
number test = new number("九万四千四百一十八");
//Test test = new Test("十八");
//Test test = new Test("八");
System.out.println(test.parse());
}
}
/**
* 中文数字其实很有规律, 一个数字, 然后跟着一个表示级别的标志,如千,百,十,万.
* 这个级别表达得不好, 但是不知道怎么表达, 知道意思就行
*
* 所以读取的时候, 读一个数字, 再读一个表示级别的标志, 计算一次
* 直到数字读完.
*
* 没有处理那个变态的五万万这种情况, 其实也不难, 在处理数字的循环中加个判断
* 这就得预先读取下一个符, 直到是数字为止, 判断其间有多少个级别标志
*/
public class Test {
// 建立数组, 用来查找各级的标志, 这一级的值为Math.pow(10, 下标 + 1);
public static char[] spliter = { '十', '百', '千', '万' };
// 建立数组, 用来查找中文中的数字与何拉伯数字的对应关系,
// 下标值为相应阿拉伯数字的值
public static char[] number = { '零', '一', '二', '三', '四', '五', '六', '七',
'八', '九' };
// 存放中文的数值
private String data = "三千四百五十一";
public Test(String data) {
this.data = data;
}
/**
* 返回中文数字的阿拉伯数字的值.
*/
public int parse() {
int result = 0;
int index = 1; // 如果第一个字符是级别标志, 这时就用到了index=1这个值
for (int i = 0; i < data.length(); ++i) {
index = 1;
if (isNumber(data.charAt(i))) {
// 是数字, 则找到在建立的中文数字的数组中的下标,
// 即为他的数值
index = find(number, data.charAt(i));
} else {
// 读到一个级别标志时, 进行计算一次
int temp = find(spliter, data.charAt(i));
result = result + index * (int) Math.pow(10, temp + 1);
}
}
// 处理最后一个字符, 如果最后一个字符是数字, 则把加上他的值
// 因为前面是读取级别标志时才进行计算一次.
int last = data.length() - 1;
if (isNumber(data.charAt(last))) {
result += find(number, data.charAt(last));
}
return result;
}
// 查找一个字符在给定数组中的位置
public static int find(char[] array, char ch) {
for (int i = 0; i < array.length; ++i) {
if (array[i] == ch) {
return i;
}
}
return -1;
}
// 字符是否数字
public static boolean isNumber(char ch) {
for (int i = 0; i < Test.number.length; ++i) {
if (ch == Test.number[i]) {
return true;
}
}
return false;
}
// 字符是否级别的标志
public static boolean isSpliter(char ch) {
for (int i = 0; i < Test.spliter.length; ++i) {
if (ch == Test.spliter[i]) {
return true;
}
}
return false;
}
public static void main(String[] args) {
Test test = new Test("九万四千四百一十八");
//Test test = new Test("十八");
//Test test = new Test("八");
System.out.println(test.parse());
}
}