栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

将数字量转换为英文单词的算法

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

将数字量转换为英文单词的算法

没多久。这是用Java编写的实现。

http://snippets.dzone.com/posts/show/3685

public class IntToEnglish {    static String[] to_19 = { "zero",  "one",   "two",  "three", "four",   "five",   "six",        "seven", "eight", "nine", "ten",   "eleven", "twelve", "thirteen",        "fourteen", "fifteen", "sixteen", "seventeen", "eighteen", "nineteen" };    static String[] tens  = { "twenty", "thirty", "forty", "fifty", "sixty", "seventy", "eighty", "ninety"};    static String[] denom = { "",        "thousand",     "million",         "billion",       "trillion",       "quadrillion",        "quintillion",  "sextillion",      "septillion",    "octillion",      "nonillion",        "decillion",    "undecillion",     "duodecillion",  "tredecillion",   "quattuordecillion",        "sexdecillion", "septendecillion", "octodecillion", "novemdecillion", "vigintillion" };    public static void main(String[] argv) throws Exception {        int tstValue = Integer.parseInt(argv[0]);        IntToEnglish itoe = new IntToEnglish();        System.out.println(itoe.english_number(tstValue));            }    // convert a value < 100 to English.    private String convert_nn(int val) throws Exception {        if (val < 20) return to_19[val];        for (int v = 0; v < tens.length; v++) { String dcap = tens[v]; int dval = 20 + 10 * v; if (dval + 10 > val) {     if ((val % 10) != 0)         return dcap + "-" + to_19[val % 10];     return dcap; }     }        throw new Exception("Should never get here, less than 100 failure");    }    // convert a value < 1000 to english, special cased because it is the level that kicks     // off the < 100 special case.  The rest are more general.  This also allows you to    // get strings in the form of "forty-five hundred" if called directly.    private String convert_nnn(int val) throws Exception {        String word = "";        int rem = val / 100;        int mod = val % 100;        if (rem > 0) { word = to_19[rem] + " hundred"; if (mod > 0) {     word = word + " "; }        }        if (mod > 0) { word = word + convert_nn(mod);        }        return word;    }    public String english_number(int val) throws Exception {        if (val < 100) { return convert_nn(val);        }        if (val < 1000) { return convert_nnn(val);        }        for (int v = 0; v < denom.length; v++) { int didx = v - 1; int dval = new Double(Math.pow(1000, v)).intValue(); if (dval > val) {     int mod = new Double(Math.pow(1000, didx)).intValue();     int l = val / mod;     int r = val - (l * mod);     String ret = convert_nnn(l) + " " + denom[didx];     if (r > 0) {         ret = ret + ", " + english_number(r);     }     return ret; }        }        throw new Exception("Should never get here, bottomed out in english_number");    }}


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/637487.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号