`
grandboy
  • 浏览: 123439 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

把字符串里中文大写数字转换成阿拉伯数字(通常在排序时使用)

阅读更多
在有的时候需要排序,但是字符串里又习惯使用"一二三。。。"这种的非阿拉伯数字的写法,所以我在网上找一段代码,根据自己的需求做了一些修改。代码如下:

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

public class ConvertBigNum {

    private static String num = "零一二三四五六七八九十";
    private static String numF = "十百千万亿";
    private static String numReg = "[零一二三四五六七八九十百千万亿]{1,}";

    private static String internalFormat(final String s, final int fixDigitals) {
        String allStr = "";
        int[] mark = getIndexBigNum(s);
        int begin = mark[0];
        int end = mark[1];
        if (begin == -1) {// 没有大写数字
            return s;
        }
        if (begin > 0) {
            allStr += s.substring(0, begin);
        }
        int pos = 1;
        int allNum = 0;
        int last = 0;
        int lastPow = 0;
        if (begin >= 0) {
            while (end > begin) {
                // 当前数字是否可以算出数据
                pos = num.indexOf(s.charAt(begin));

                if (pos >= 0 && pos != 10) {
                    allNum += pos;
                    if (begin != 0 && num.indexOf(s.charAt(begin - 1)) != 10) {
                        last = pos;
                    } else {
                        last += pos;
                    }

                } else {
                    pos = numF.indexOf(s.charAt(begin)) + 1;
                    // 当前的为numF
                    if (pos > 0) {
                        if (pos >= 4) {
                            // 万以上特殊处理
                            last = 0;
                            allNum += (int) (Math.pow(10, pos) * (allNum - lastPow)) - (allNum - lastPow);
                            lastPow = allNum;

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

                    }
                }

                begin++;

            }

        }

        if (allNum > 0) {
            if (fixDigitals > 0) {
                allStr += String.format("%0" + fixDigitals + "d", allNum);
            } else {
                allStr += allNum;
            }

        }

        if (begin < s.length()) {
            allStr += s.substring(begin);
        }
        return allStr;
    }

    private static int[] getIndexBigNum(String s) {
        if (s != null) {
            Matcher matcher = Pattern.compile(numReg).matcher(s);
            if (matcher.find()) {
                return new int[] { matcher.start(), matcher.end() };
            } else {
                return new int[] { -1, -1 };
            }
        } else {
            return new int[] { -1, -1 };
        }
    }

    public static String format(final String ori, final int fixDigitals) {
        String s = ori;
        int[] mark = getIndexBigNum(s);
        int begin = mark[0];
        if (begin > -1) {
            do {
                s = internalFormat(s, fixDigitals);
                mark = getIndexBigNum(s);
                begin = mark[0];
            } while (begin > -1);
        }
        return s;
    }

    public static String format(final String ori) {
        return format(ori, 0);
    }

    public static void main(String[] args) {
        System.out.println(format("第三年级第二十一A班", 3));
        System.out.println(format("五万二千三百年二班", 5));
    }
}
0
1
分享到:
评论

相关推荐

    linux_c API函数大全

    atof(将字符串转换成浮点型数) 21 2.2 22 atoi(将字符串转换成整型数) 22 2.3 22 atol(将字符串转换成长整型数) 22 2.4 23 gcvt(将浮点型数转换为字符串,取四舍五入) 23 2.5 24 strtod(将字符串转换成...

    Excel百宝箱9.0无限制破解版.rar

    函数功能与参数:与“大写”函数相反,将大写字符转换为阿拉伯数字。 函数名称:分割取数 函数功能与参数:按分割符取数。第一参数为单元格,第二参数表示取第N个,第三个参数表示分割符。 函数名称:共有项 ...

    Exce百宝箱——2012版本.rar

    函数功能与参数:与“大写”函数相反,将大写字符转换为阿拉伯数字。 函数名称:分割取数 函数功能与参数:按分割符取数。第一参数为单元格,第二参数表示取第N个,第三个参数表示分割符。 函数名称:共有项 函数...

    Excel百宝箱 9.0 破解版 批量导入图片等200种功能

    数字与“大写”函数相反,将大写字符转换为阿拉伯数字 分割取数按分割符取数。第一参数为单元格,第二参数表示取第N个,第三个参数表示分割符 共有项返回两个区域共有项目。前两个参数为区域,第三参数为序号 不同项...

    EXCEL百宝箱8.0终极版

    函数功能与参数:与“大写”函数相反,将大写字符转换为阿拉伯数字。 函数名称:分割取数 函数功能与参数:按分割符取数。第一参数为单元格,第二参数表示取第N个,第三个参数表示分割符。 函数名称:共有项 函数...

    Excel百宝箱8.0

    函数功能与参数:与“大写”函数相反,将大写字符转换为阿拉伯数字。 函数名称:分割取数 函数功能与参数:按分割符取数。第一参数为单元格,第二参数表示取第N个,第三个参数表示分割符。 函数名称:共有项 函数...

    大量批处理实用程序例程

    中文数字转换为阿拉伯数字.cmd 人民币大小写互转程序.cmd 人民币金额小写转大写.cmd 十进制转二进制.cmd 十进制转十六进制.cmd 字母大小写转换.cmd 把秒转换为天小时分秒的格式.cmd 阿拉伯与罗马数字互转程序.cmd ...

    700个批处理打包下载.rar

    中文数字转换为阿拉伯数字.cmd 中文显示ping结果.bat 九九乘法表.cmd 九宫格图案.cmd 乱序重列某类文件.cmd 交换两个变量的值而不使用临时变量.bat 交换两个变量的值而不使用临时变量.cmd 人民币大小写互转程序.cmd ...

    1345个易语言模块

    API的使用方法.ec API 设置颜色对话框模块.ec ard.ec Arhz_自动更新.ec Base64编解码.ec BASE64编解码模块.ec Bios.ec Bios 信息.ec BMP加密数据.ec BMP滤镜模块.ec BOX.EC BPL专用更新模块.ec BPL综合模 块.ec BPL...

    1350多个精品易语言模块

    API的使用方法.ec API 设置颜色对话框模块.ec ard.ec Arhz_自动更新.ec Base64编解码.ec BASE64编解码模块.ec Bios.ec Bios 信息.ec BMP加密数据.ec BMP滤镜模块.ec BOX.EC BPL专用更新模块.ec BPL综合模 块.ec BPL...

Global site tag (gtag.js) - Google Analytics