数字的英文表达和中文表达

翻译 2018年04月17日 17:30:03

这里写图片描述

//数字的英文表达和中文表达
public class NumEngAndChinese{

    //*************************中文表达************************************
   public static String num1To9(int num) {
        if (num < 1 || num > 9) {
            return "";
        }
        String[] names = { "一", "二", "三", "四", "五", "六", "七", "八", "九" };
        return names[num - 1];
    }

    public static String num1To99(int num, boolean hasBai) {
        if (num < 1 || num > 99) {
            return "";
        }
        if (num < 10) {
            return num1To9(num);
        }
        int shi = num / 10;
        if (shi == 1 && (!hasBai)) {
            return "十" + num1To9(num % 10);
        } else {
            return num1To9(shi) + "十" + num1To9(num % 10);
        }
    }

    public static String num1To999(int num) {
        if (num < 1 || num > 999) {
            return "";
        }
        if (num < 100) {
            return num1To99(num, false);
        }
        String res = num1To9(num / 100) + "百";
        int rest = num % 100;
        if (rest == 0) {
            return res;
        } else if (rest >= 10) {
            res += num1To99(rest, true);
        } else {
            res += "零" + num1To9(rest);
        }
        return res;
    }

    public static String num1To9999(int num) {
        if (num < 1 || num > 9999) {
            return "";
        }
        if (num < 1000) {
            return num1To999(num);
        }
        String res = num1To9(num / 1000) + "千";
        int rest = num % 1000;
        if (rest == 0) {
            return res;
        } else if (rest >= 100) {
            res += num1To999(rest);
        } else {
            res += "零" + num1To99(rest, false);
        }
        return res;
    }

    public static String num1To99999999(int num) {
        if (num < 1 || num > 99999999) {
            return "";
        }
        int wan = num / 10000;
        int rest = num % 10000;
        if (wan == 0) {
            return num1To9999(rest);
        }
        String res = num1To9999(wan) + "万";
        if (rest == 0) {
            return res;
        } else {
            if (rest < 1000) {
                return res + "零" + num1To999(rest);
            } else {
                return res + num1To9999(rest);
            }
        }
    }

    public static String getNumChiExp(int num) {
        if (num == 0) {
            return "零";
        }
        String res = num < 0 ? "负" : "";
        int yi = Math.abs(num / 100000000);
        int rest = Math.abs((num % 100000000));
        if (yi == 0) {
            return res + num1To99999999(rest);
        }
        res += num1To9999(yi) + "亿";
        if (rest == 0) {
            return res;
        } else {
            if (rest < 10000000) {
                return res + "零" + num1To99999999(rest);
            } else {
                return res + num1To99999999(rest);
            }
        }
    }


    //*****************************************************

    //***********************英文表达**********************
        public static String num1To19(int num) {
        if (num < 1 || num > 19) {
            return "";
        }
        String[] names = { "One ", "Two ", "Three ", "Four ", "Five ", "Six ",
                "Seven ", "Eight ", "Nine ", "Ten ", "Eleven ", "Twelve ",
                "Thirteen ", "Fourteen ", "Fifteen ", "Sixteen ", "Sixteen ",
                "Eighteen ", "Nineteen " };
        return names[num - 1];
    }

    public static String num1To99(int num) {
        if (num < 1 || num > 99) {
            return "";
        }
        if (num < 20) {
            return num1To19(num);
        }
        int high = num / 10;
        String[] tyNames = { "Twenty ", "Thirty ", "Forty ", "Fifty ",
                "Sixty ", "Seventy ", "Eighty ", "Ninety " };
        return tyNames[high - 2] + num1To19(num % 10);
    }

    public static String num1ToE999(int num) {
        if (num < 1 || num > 999) {
            return "";
        }
        if (num < 100) {
            return num1To99(num);
        }
        int high = num / 100;
        return num1To19(high) + "Hundred " + num1To99(num % 100);
    }
     //获得英文的表达式
    public static String getNumEngExp(int num) {
        if (num == 0) {
            return "Zero";
        }
        String res = "";
        if (num < 0) {
            res = "Negative, ";
        }
        if (num == Integer.MIN_VALUE) {
            res += "Two Billion, ";
            num %= -2000000000;
        }
        num = Math.abs(num);
        int high = 1000000000;
        int highIndex = 0;
        String[] names = { "Billion", "Million", "Thousand", "" };
        while (num != 0) {
            int cur = num / high;
            num %= high;
            if (cur != 0) {
                res += num1ToE999(cur);
                res += names[highIndex] + (num == 0 ? " " : ", ");
            }
            high /= 1000;
            highIndex++;
        }
        return res;
    }

    //*****************************************************
    // for test
    public static int generateRandomNum() {
        boolean isNeg = Math.random() > 0.5 ? false : true;
        int value = (int) (Math.random() * Integer.MIN_VALUE);
        return isNeg ? value : -value;
    }
    public static void main(String[]args)
    {

        System.out.println(0);
        System.out.println(getNumChiExp(0));

        System.out.println(Integer.MAX_VALUE);
        System.out.println(getNumChiExp(Integer.MAX_VALUE));

        System.out.println(Integer.MIN_VALUE);
        System.out.println(getNumChiExp(Integer.MIN_VALUE));

        int num = generateRandomNum();
        System.out.println(num);
        System.out.println(getNumChiExp(num));

        num = generateRandomNum();
        System.out.println(num);
        System.out.println(getNumChiExp(num));

        num = generateRandomNum();
        System.out.println(num);
        System.out.println(getNumChiExp(num));

        num = generateRandomNum();
        System.out.println(num);
        System.out.println(getNumChiExp(num));

        System.out.println(getNumChiExp(10));
        System.out.println(getNumChiExp(110));
        System.out.println(getNumChiExp(1010));
        System.out.println(getNumChiExp(10010));
        System.out.println(getNumChiExp(1900000000));
        System.out.println(getNumChiExp(1000000010));
        System.out.println(getNumChiExp(1010100010));

         //******************************************
        System.out.println(getNumEngExp(0));
        System.out.println(getNumEngExp(Integer.MAX_VALUE));
        System.out.println(getNumEngExp(Integer.MIN_VALUE));
        int num2 = generateRandomNum();
        System.out.println(num2);
        System.out.println(getNumEngExp(num2));



    }
}

这里写图片描述

英语数字表达方式大全

基数词0 nought;zero;O1 one2 two3 three4 four5 five6 six7 seven8 eight9 nine10 ten11 eleven12 twelve13 t...
  • jenhy
  • jenhy
  • 2008-04-16 09:13:00
  • 51020

英语各种数字表达方式大全(转载)

基数词 0 nought;zero;O 1 one 2 two 3 three 4 four 5 five 6 six 7 seven 8 eight 9 nine 10 ten...
  • sun7545526
  • sun7545526
  • 2012-10-28 22:53:53
  • 25892

中文最常用600句短语地道英文表达

中文最常用600句短语地道英文表达 感觉很有用,需要时可以来参考参考.2 字篇1. 活该! serves you(him,her) right!   = you deserve (he/she des...
  • lddongyu
  • lddongyu
  • 2007-06-21 14:54:00
  • 1406

其他题目---数字的英文表达和中文表达

【题目】  给定一个32位整数n,写两个函数分别返回n的英文与中文表达字符串。【基本思路】  该类型的题目通常是由小的,简单的场景出发,把复杂的事情拆解成简单的场景。  英文表达的实现。英文的表达是以...
  • qq_34342154
  • qq_34342154
  • 2017-11-01 11:05:21
  • 163

中文最常用600句口语地道英文表达

  • 2009年09月02日 17:18
  • 605KB
  • 下载

常见数学公式的英文表达

  • 2009年03月08日 21:01
  • 83KB
  • 下载

数学公式的英语表达

加:plus 减:minus 乘:times/multiplied by 除:divided by (不)等于:(not)equals/be (not) equ...
  • xuexiang0704
  • xuexiang0704
  • 2012-12-04 16:22:00
  • 4214

将中文数字转换成英文表示

将中文数字转换成英文表示,例:1031转换成 one thousand thirty and one
  • ankeyuan
  • ankeyuan
  • 2014-10-03 10:24:39
  • 1413

论文中常用英语表达

利用: use, exploit, utilize, leverage 我们的模型具有通用性: Our model is general. Our proposed model has an a...
  • u013713117
  • u013713117
  • 2017-02-11 10:26:32
  • 628

中国一些流行词语的英文表达

  • 2009年04月09日 11:22
  • 50KB
  • 下载
收藏助手
不良信息举报
您举报文章:数字的英文表达和中文表达
举报原因:
原因补充:

(最多只允许输入30个字)