找个地方存代码_大写数字转阿拉伯

含有大写数字的视频标题排序不理想,特转换后比较

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Compare {

public static String num = "零一二三四五六七八九十";
public static String numF = " 十百千万 亿";

public static String numReg = "[零一二三四五六七八九十]";
public static String numFReg = "[十百千万亿]";
//public static String numF[][] = {{"十","1"} , {"百" , "2"} , {"千" , "3"} , {""}};
public static int compare(String s1 , String s2){

int i = format(s1) - format(s2);
if(i == 0){
return s1.compareTo(s2);
}else {
return i;
}


}

public static int format(String s){

int mark = hasNum(s);
int value = 1;
int all = 0;
int last = 0;
int lastPow = 0;
if(mark >= 0){

while (s.length() > mark) {


//当前数字是否可以算出数据
value = num.indexOf(s.charAt(mark));
if(value >= 0 && value != 10){
all += value;
if(mark !=0 && num.indexOf(s.charAt(mark -1)) != 10){
last = value;
}else {
last += value;
}


}else {
value = numF.indexOf(s.charAt(mark));
//当前的为numF
if(value > 0){
if(value >=4){
//万以上特殊处理

last = 0;
all += (int)(Math.pow(10, value) * (all - lastPow)) - (all - lastPow);
lastPow = all;

}else {
if(last > 0){
//前面是数字
all -= last;
last = (int)(last * Math.pow(10, value));
all += last;
}else {
last = (int)(Math.pow(10, value));;
all += last;

}
}

}
}

mark++;

}

}
return all;
}

public static int hasNum(String s) {

if(s != null){

Matcher matcher = Pattern.compile(numReg).matcher(s);
if(matcher.find())
{

return matcher.start();
}
else {
return -1;
}
}
else {
return -1;
}
}
public static void main(String[] args) {




System.out.println(format("十八亿三千万零九百六十四"));
System.out.println(format("九十九万六千七百八十三"));


}
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值