分享

Java数字转中文大写,数字转英文

 hehffyy 2014-03-12

这里贴的是refactor后的最新版,新版支持传入String类型的数字。


这里是一个示例程序及输出:

  1. import java.util.Arrays;  
  2. import java.util.List;  
  3.   
  4. public class NumberTextTest {  
  5.     
  6.   public static void main(String[] args) {  
  7.       
  8.     List<String> numbers = Arrays.asList(  
  9.             "1",  
  10.             "123",  
  11.             "123456789",  
  12.             "101",  
  13.             "1001",  
  14.             "100000",  
  15.             "1000300000250000004",  
  16.             "205734908570001",  
  17.             "1348900"  
  18.     );  
  19.       
  20.     NumberText nt = NumberText.getInstance(NumberText.Lang.ChineseSimplified);  
  21.     for(String number : numbers)  
  22.       System.out.println(nt.getText(number));  
  23.       
  24.     System.out.println("---");  
  25.       
  26.     nt = NumberText.getInstance(NumberText.Lang.English);  
  27.     for(String number : numbers)  
  28.       System.out.println(nt.getText(number));  
  29.   }  
  30. }  

输出如下:

  1. run:  
  2. 一  
  3. 一百二十三  
  4. 一亿二千三百四十五万六千七百八十九  
  5. 一百零一  
  6. 一千零一  
  7. 一十万  
  8. 一百京零三百兆零二亿五千万零四  
  9. 二百零五兆七千三百四十九亿零八百五十七万零一  
  10. 一百三十四万八千九百  
  11. ---  
  12. one  
  13. one hundred and twenty three  
  14. one hundred and twenty three million four hundred and fifty six thousand seven hundred and eighty nine  
  15. one hundred and one  
  16. one thousand and one  
  17. one hundred thousand   
  18. one quintillion three hundred trillion two hundred and fifty million and four  
  19. two hundred and five trillion seven hundred and thirty four billion nine hundred and eight million five hundred and seventy thousand and one  
  20. one million three hundred and fourty eight thousand nine hundred  
  21. BUILD SUCCESSFUL (total time: 0 seconds)  


另外这个工具还支持中文大写:

  1. NumberText.getInstance(NumberText.Lang.ChineseTraditional);  

NumberText.java 的源代码如下:


  1. /*  
  2.  * Copyright 2012 na.shi.wu.you (raistlic@gmail.com)  
  3.  *  
  4.  * Licensed under the Apache License, Version 2.0 (the "License");  
  5.  * you may not use this file except in compliance with the License.  
  6.  * You may obtain a copy of the License at  
  7.  *  
  8.  * http://www./licenses/LICENSE-2.0  
  9.  *  
  10.  * Unless required by applicable law or agreed to in writing, software  
  11.  * distributed under the License is distributed on an "AS IS" BASIS,  
  12.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  
  13.  * See the License for the specific language governing permissions and  
  14.  * limitations under the License.  
  15.  */  
  16. import java.util.HashMap;  
  17. import java.util.Map;  
  18.   
  19. /** 
  20.  * This class transfers an integer number into a string : 
  21.  *  
  22.  * <br/> 
  23.  *  
  24.  * <pre>{@code 
  25.  * // an example : 
  26.  *   NumberText ns = NumberText.getInstance(NumberText.Lang.EnglishWithDash); 
  27.  *   ns.getText(123) // one hundred and twenty-three 
  28.  *   ns.getOrdinalText(320) // three hundred and twentieth 
  29.  * }</pre> 
  30.  *  
  31.  * @date   09/02/2012 
  32.  * @author na.shi.wu.you (raistlic@gmail.com) 
  33.  */  
  34. public abstract class NumberText {  
  35.     
  36.   /*---------------------------------------------------------------------------- 
  37.    * not designed to be inherented outside this file 
  38.    * no public constructors provided, -- use factory method 
  39.    ---------------------------------------------------------------------------*/  
  40.   private NumberText() {}  
  41.     
  42.   /** 
  43.    * Exports a {@code NumberText} implementation instance, based on a natural  
  44.    * language argument. {@see Lang} 
  45.    *  
  46.    * @param lang 
  47.    * @return a NumberText instance. 
  48.    */  
  49.   public static NumberText getInstance(Lang lang) {  
  50.       
  51.     return lang.instance();  
  52.   }  
  53.     
  54.   /** 
  55.    * Transfers an integer number into a String, specifically in which language  
  56.    * depends on the implementation. 
  57.    * <p /> 
  58.    * e.g. in EnglishWithDash, 
  59.    * <p /> 
  60.    *   100 -> one hundred 
  61.    * <br /> 
  62.    *   -976083854 -> minus nine hundred and seventy-six million and eighty-three  
  63.    *                 thousand eight hundred and fifty-four 
  64.    *  
  65.    * @param number the integer number to be transfered 
  66.    * @return the result String 
  67.    */  
  68.   public final String getText(long number) {  
  69.       
  70.     return getText(Long.toString(number));  
  71.   }  
  72.     
  73.   /** 
  74.    * Transfers an integer number into a String, specifically in which language  
  75.    * depends on the implementation. 
  76.    * <p /> 
  77.    * e.g. in EnglishWithDash, 
  78.    * <p /> 
  79.    *   100 -> one hundred 
  80.    * <br /> 
  81.    *   -976083854 -> minus nine hundred and seventy-six million and eighty-three  
  82.    *                 thousand eight hundred and fifty-four 
  83.    *  
  84.    * @param number the integer number to be transfered 
  85.    * @return the result String 
  86.    */  
  87.   public abstract String getText(String number);  
  88.     
  89.   /** 
  90.    * Transfers an integer number into a String of its ordinal representation,  
  91.    * specifically in which language depends on the implementation. 
  92.    * <p /> 
  93.    * e.g. in EnglishWithDash, 
  94.    * <p /> 
  95.    *   100 -> one hundredth 
  96.    * <br /> 
  97.    *   8331125 -> eight million three hundred and thirty-one thousand one  
  98.    *              hundred and twenty-fifth 
  99.    *  
  100.    * @param number the integer number to be transfered 
  101.    * @return the result String 
  102.    */  
  103.   public final String getOrdinalText(long number) {  
  104.       
  105.     return getOrdinalText(Long.toString(number));  
  106.   }  
  107.     
  108.   /** 
  109.    * <p> 
  110.    * Transfers an integer number into a String of its ordinal representation,  
  111.    * specifically in which language depends on the implementation. 
  112.    * </p> 
  113.    *  
  114.    * <p> 
  115.    * e.g. in EnglishWithDash, 
  116.    * <br /><br /> 
  117.    *   100 -> one hundredth 
  118.    * <br /> 
  119.    *   8331125 -> eight million three hundred and thirty-one thousand one  
  120.    *              hundred and twenty-fifth 
  121.    * </p> 
  122.    *  
  123.    * @param number the integer number to be transfered 
  124.    * @return the result String 
  125.    */  
  126.   public abstract String getOrdinalText(String number);  
  127.     
  128.   /** 
  129.    * This enumeration type is typically named under a natural language  
  130.    * name, and is to mark a specific implementation name; it is used as an  
  131.    * argument to call the factory method  
  132.    * {@link NumberText#getInstance(NumberText.Lang)}. 
  133.    */  
  134.   public static enum Lang {  
  135.       
  136.     English              (NumberTextEnglishCleanSpaceOnly.INSTANCE),  
  137.     EnglishWithDash      (NumberTextEnglish.INSTANCE),  
  138.     ChineseSimplified    (NumberTextChinese.SIMPLIFIED),  
  139.     ChineseTraditional   (NumberTextChinese.TRADITIONAL),  
  140.     ;  
  141.       
  142.     private final NumberText instance;  
  143.     private Lang(NumberText instance) {  
  144.         
  145.       this.instance = instance;  
  146.     }  
  147.       
  148.     private NumberText instance() {  
  149.         
  150.       if( instance == null )  
  151.         throw new UnsupportedOperationException(  
  152.                 "Language not supported yet : " + this);  
  153.         
  154.       return instance;  
  155.     }  
  156.   }  
  157.     
  158.   abstract int limit();  
  159.     
  160.   void checkNumber(String number) {  
  161.       
  162.     if( !number.matches("-?\\d+") )  
  163.       throw new NumberFormatException();  
  164.       
  165.     int length = number.length();  
  166.     if( number.startsWith("-") )  
  167.       length --;  
  168.       
  169.     if( length > limit() )  
  170.       throw new UnsupportedOperationException(  
  171.               "The current " + NumberText.class.getSimpleName() +  
  172.               "can only handle numbers up to (+/-)10^" + limit() + ".");  
  173.   }  
  174.     
  175.   /*---------------------------------------------------------------------------- 
  176.    * EnglishWithDash Implementation 
  177.    ---------------------------------------------------------------------------*/  
  178.   private static class NumberTextEnglish extends NumberText {  
  179.       
  180.     private static final NumberText INSTANCE = new NumberTextEnglish();  
  181.       
  182.     static enum Connect {  
  183.         
  184.       Minus               ("minus"),  
  185.       Hundred             ("hundred"),  
  186.       And                 ("and"),  
  187.       AfterMinus          (" "),  
  188.       AfterNumber         (" "),  
  189.       AfterPower          (" "),  
  190.       AfterHundred        (" "),  
  191.       AfterAnd            (" "),  
  192.       AfterTen            ("-"),  
  193.       ;  
  194.         
  195.       final String display;  
  196.       Connect(String display) { this.display = display; }  
  197.         
  198.       private static boolean isConnect(char c) {  
  199.         return c == ' ' || c == '-';  
  200.       }  
  201.     }  
  202.       
  203.     static enum Power {  
  204.   
  205.       Thousand          ("thousand"),          // 10 ^ 3  
  206.       Million           ("million"),           // 10 ^ 6  
  207.       Billion           ("billion"),           // 10 ^ 9  
  208.       Trillion          ("trillion"),          // 10 ^ 12  
  209.       Quadrillion       ("quadrillion"),       // 10 ^ 15  
  210.       Quintillion       ("quintillion"),       // 10 ^ 18 (enough for Long.MAX_VALUE)  
  211.       Sextillion        ("sextillion"),        // 10 ^ 21  
  212.       Septillion        ("septillion"),        // 10 ^ 24  
  213.       Octillion         ("octillion"),         // 10 ^ 27  
  214.       Nonillion         ("nonillion"),         // 10 ^ 30  
  215.       Decillion         ("decillion"),         // 10 ^ 33  
  216.       Undecillion       ("undecillion"),       // 10 ^ 36  
  217.       Duodecillion      ("duodecillion"),      // 10 ^ 39  
  218.       Tredecillion      ("tredecillion"),      // 10 ^ 42  
  219.       Quattuordecillion ("quattuordecillion"), // 10 ^ 45  
  220.       Quindecillion     ("quindecillion"),     // 10 ^ 48  
  221.       Sexdecillion      ("sexdecillion"),      // 10 ^ 51  
  222.       Septendecillion   ("septendecillion"),   // 10 ^ 54  
  223.       Octodecillion     ("octodecillion"),     // 10 ^ 57  
  224.       Novemdecillion    ("novemdecillion"),    // 10 ^ 60  
  225.       Vigintillion      ("vigintillion"),      // 10 ^ 63  
  226.       ;  
  227.         
  228.       final String display;  
  229.       Power(String display) { this.display = display; }  
  230.     }  
  231.       
  232.     static enum Digit {  
  233.   
  234.       Zero  ("zero""zeroth""ten"""),  
  235.       One   ("one""first""eleven""ten"),  
  236.       Two   ("two""second""twelve""twenty"),  
  237.       Three ("three""third""thirteen""thirty"),  
  238.       Four  ("four""fourth""fourteen""fourty"),  
  239.       Five  ("five""fifth""fifteen""fifty"),  
  240.       Six   ("six""sixth""sixteen""sixty"),  
  241.       Seven ("seven""seventh""seventeen""seventy"),  
  242.       Eight ("eight""eighth""eighteen""eighty"),  
  243.       Nine  ("nine""nineth""nineteen""ninety"),  
  244.       ;  
  245.         
  246.       final String display, displayOrdinal, plusTen, multiTen;  
  247.       Digit(String display, String displayOrdinal,   
  248.             String plusTen, String multiTen) {  
  249.         this.display = display;  
  250.         this.displayOrdinal = displayOrdinal;  
  251.         this.plusTen = plusTen;  
  252.         this.multiTen = multiTen;  
  253.       }  
  254.     }  
  255.       
  256.     private static final Map<String, String> _Ordinals;  
  257.     static {  
  258.       _Ordinals = new HashMap<String, String>();  
  259.       for(Digit d : Digit.values())  
  260.         _Ordinals.put(d.display, d.displayOrdinal);  
  261.     }  
  262.       
  263.     @Override  
  264.     int limit() {  
  265.         
  266.       return 63;  
  267.     }  
  268.   
  269.     @Override  
  270.     public String getText(String number) {  
  271.         
  272.       checkNumber(number);  
  273.         
  274.       StringBuilder builder = new StringBuilder();  
  275.       buildText(builder, number);  
  276.       return builder.toString();  
  277.     }  
  278.   
  279.     @Override  
  280.     public String getOrdinalText(String number) {  
  281.         
  282.       checkNumber(number);  
  283.         
  284.       StringBuilder builder = new StringBuilder();  
  285.       buildText(builder, number);  
  286.       replaceLastTokenWithOrdinal(builder);  
  287.       return builder.toString();  
  288.     }  
  289.       
  290.     private void buildText(StringBuilder builder, String number) {  
  291.         
  292.       assert builder != null;  
  293.         
  294.       if( number.startsWith("-") ) {  
  295.         builder.append(getConnectDisplay(Connect.Minus))  
  296.                .append(getConnectDisplay(Connect.AfterMinus));  
  297.         number = number.substring(1);  
  298.       }  
  299.         
  300.       int power = 0;  
  301.       while(number.length() > (power + 1) * 3)  
  302.         power++;  
  303.         
  304.       while(power > 0) {  
  305.         boolean modified = extendToken(builder, number, power * 3);  
  306.         if( modified )  
  307.           builder.append(getConnectDisplay(Connect.AfterNumber))  
  308.                  .append(getPowerDisplay(Power.values()[power-1]));  
  309.         power--;  
  310.       }  
  311.       extendToken(builder, number, 0);  
  312.     }  
  313.       
  314.     private boolean extendToken(StringBuilder builder,   
  315.                                 String number,   
  316.                                 int suffix) {  
  317.         
  318.       assert builder != null && suffix < number.length();  
  319.         
  320.       int len = number.length() - suffix;  
  321.       int hundreds = len > 2 ? (int)(number.charAt(len-3)-'0') : -1;  
  322.       int tens = len > 1 ? (int)(number.charAt(len-2)-'0') : -1;  
  323.       int inds = (int)(number.charAt(len-1)-'0');  
  324.         
  325.       if( hundreds <= 0 && tens <= 0 && inds <= 0 && suffix > 0 )  
  326.         return false;  
  327.       else if( len > 3 )  
  328.         builder.append(getConnectDisplay(Connect.AfterPower));  
  329.         
  330.       if( hundreds == 0 ) {  
  331.         if( len > 3 && (tens > 0 || inds > 0) )  
  332.           builder.append(getConnectDisplay(Connect.And))  
  333.                  .append(getConnectDisplay(Connect.AfterAnd));  
  334.       }  
  335.       else if( hundreds > 0 ) {  
  336.         builder.append(getDigitName(Digit.values()[hundreds]))  
  337.                .append(getConnectDisplay(Connect.AfterNumber))  
  338.                .append(getConnectDisplay(Connect.Hundred));  
  339.         if( tens > 0 || inds > 0 )   
  340.           builder.append(getConnectDisplay(Connect.AfterHundred))  
  341.                  .append(getConnectDisplay(Connect.And))  
  342.                  .append(getConnectDisplay(Connect.AfterAnd));  
  343.       }  
  344.         
  345.       if( tens > 1 ) {  
  346.         builder.append(getDigitMultiTen(Digit.values()[tens]));  
  347.         if( inds > 0 )  
  348.           builder.append(getConnectDisplay(Connect.AfterTen));  
  349.       }  
  350.         
  351.       if( tens == 1 )  
  352.         builder.append(getDigitPlusTen(Digit.values()[inds]));  
  353.       else if( inds > 0 || number.length() == 1 )  
  354.         builder.append(getDigitName(Digit.values()[inds]));  
  355.         
  356.       return true;  
  357.     }  
  358.       
  359.     private void replaceLastTokenWithOrdinal(StringBuilder builder) {  
  360.         
  361.       assert builder != null && builder.length() > 0;  
  362.         
  363.       int suffix = builder.length()-1;  
  364.       while( suffix >= 0 && !isConnect(builder.charAt(suffix)) )   
  365.         suffix--;  
  366.       String lastToken = builder.substring(suffix+1);  
  367.       builder.delete(suffix+1, builder.length()).append(toOrdinal(lastToken));  
  368.     }  
  369.       
  370.     String getPowerDisplay(Power power) {  
  371.         
  372.       assert power != null;  
  373.         
  374.       return power.display;   
  375.     }  
  376.       
  377.     String getConnectDisplay(Connect connect) {  
  378.         
  379.       assert connect != null;  
  380.         
  381.       return connect.display;  
  382.     }  
  383.       
  384.     String getDigitName(Digit digit) {  
  385.         
  386.       assert digit != null;  
  387.         
  388.       return digit.display;  
  389.     }  
  390.       
  391.     String toOrdinal(String name) {  
  392.         
  393.       assert name != null && !name.isEmpty();  
  394.         
  395.       String result = _Ordinals.get(name);  
  396.       if( result == null ) {  
  397.         if( name.charAt(name.length()-1) == 'y' )  
  398.           result = name.substring(0, name.length()-1) + "ieth";  
  399.         else  
  400.           result = name + "th";  
  401.       }  
  402.       return result;  
  403.     }  
  404.       
  405.     String getDigitPlusTen(Digit digit) {  
  406.         
  407.       assert digit != null;  
  408.         
  409.       return digit.plusTen;  
  410.     }  
  411.       
  412.     String getDigitMultiTen(Digit digit) {  
  413.         
  414.       assert digit != null;  
  415.         
  416.       return digit.multiTen;  
  417.     }  
  418.       
  419.     boolean isConnect(char c) {  
  420.       return Connect.isConnect(c);  
  421.     }  
  422.   }  
  423.     
  424.   /*---------------------------------------------------------------------------- 
  425.    * EnglishWithDash with only Clean Space Connectors 
  426.    ---------------------------------------------------------------------------*/  
  427.   private static class NumberTextEnglishCleanSpaceOnly   
  428.   extends NumberTextEnglish {  
  429.       
  430.     private static final NumberText INSTANCE =   
  431.             new NumberTextEnglishCleanSpaceOnly();  
  432.       
  433.     @Override  
  434.     String getConnectDisplay(Connect connect) {  
  435.         
  436.       return connect == Connect.AfterTen ?   
  437.              " " :   
  438.              super.getConnectDisplay(connect);  
  439.     }  
  440.   }  
  441.     
  442.   /*---------------------------------------------------------------------------- 
  443.    * ChineseSimplified Implementation 
  444.    ---------------------------------------------------------------------------*/  
  445.   private static class NumberTextChinese extends NumberText {  
  446.       
  447.     private static final NumberText SIMPLIFIED =   
  448.             new NumberTextChinese(Type.Simplified);  
  449.     private static final NumberText TRADITIONAL =  
  450.             new NumberTextChinese(Type.Traditional);  
  451.               
  452.     static enum Type { Simplified, Traditional; }  
  453.       
  454.     static enum Connect {  
  455.       Di     ("第""第"),  
  456.       Fu     ("负""負"),  
  457.       Ling   ("零""零"),  
  458.       Shi    ("十""拾"),  
  459.       Bai    ("百""佰"),  
  460.       Qian   ("千""仟"),  
  461.       ;  
  462.   
  463.       final String display, displayTraditional;  
  464.       Connect(String display, String displayTraditional) {   
  465.         this.display = display;   
  466.         this.displayTraditional = displayTraditional;  
  467.       }  
  468.     }  
  469.   
  470.     static enum Power {  
  471.   
  472.       Wan    ("万""萬"), // 10^4  
  473.       Yi     ("亿""億"), // 10^8  
  474.       Zhao   ("兆""兆"), // 10^12  
  475.       Jing   ("京""京"), // 10^16 (enough for Long.MAX_VALUE)  
  476.       Gai    ("垓""垓"), // 10^20  
  477.       Zi     ("秭""秭"), // 10^24  
  478.       Rang   ("穰""穰"), // 10^28  
  479.       Gou    ("沟""溝"), // 10^32  
  480.       Jian   ("涧""澗"), // 10^36  
  481.       Zheng  ("正""正"), // 10^40  
  482.       Zai    ("载""載"), // 10^44  
  483.       ;  
  484.   
  485.       final String display, displayTraditional;  
  486.       Power(String display, String displayTraditional) {   
  487.         this.display = display;   
  488.         this.displayTraditional = displayTraditional;  
  489.       }  
  490.     }  
  491.       
  492.     static enum Digit {  
  493.          
  494.        Ling   ("零""零"), // just to occupy this position  
  495.        Yi     ("一""壹"),  
  496.        Er     ("二""贰"),  
  497.        San    ("三""叁"),  
  498.        Si     ("四""肆"),  
  499.        Wu     ("五""伍"),  
  500.        Liu    ("六""陆"),  
  501.        Qi     ("七""柒"),  
  502.        Ba     ("八""捌"),  
  503.        Jiu    ("九""玖"),  
  504.        ;  
  505.          
  506.        final String display, displayTraditional;  
  507.        Digit(String display, String displayTraditional) {   
  508.          this.display = display;   
  509.          this.displayTraditional = displayTraditional;  
  510.        }  
  511.      }  
  512.       
  513.     private final Type type;  
  514.     private NumberTextChinese(Type type) {  
  515.       assert type != null;  
  516.         
  517.       this.type = type;  
  518.     }  
  519.       
  520.     @Override  
  521.     int limit() {  
  522.         
  523.       return 44;  
  524.     }  
  525.       
  526.     @Override  
  527.     public String getText(String number) {  
  528.         
  529.       checkNumber(number);  
  530.         
  531.       StringBuilder builder = new StringBuilder();  
  532.       buildText(builder, number);  
  533.       return builder.toString();  
  534.     }  
  535.   
  536.     @Override  
  537.     public String getOrdinalText(String number) {  
  538.         
  539.       checkNumber(number);  
  540.         
  541.       StringBuilder builder = new StringBuilder().append(Connect.Di);  
  542.       buildText(builder, number);  
  543.       return builder.toString();  
  544.     }  
  545.       
  546.     private void buildText(StringBuilder builder, String number) {  
  547.         
  548.       assert builder != null;  
  549.         
  550.       if( number.startsWith("-") ) {  
  551.         builder.append(getConnectDisplay(Connect.Fu));  
  552.         number = number.substring(1);  
  553.       }  
  554.         
  555.       int power = 0;  
  556.       while(number.length() > (power + 1) * 4)  
  557.         power++;  
  558.         
  559.       while(power > 0) {  
  560.         if( extendToken(builder, number, power * 4) )  
  561.           builder.append(getPowerDisplay(Power.values()[power-1]));  
  562.         power--;  
  563.       }  
  564.       extendToken(builder, number, 0);  
  565.     }  
  566.       
  567.     private boolean extendToken(StringBuilder builder,   
  568.                                 String number,   
  569.                                 int suffix) {  
  570.         
  571.       assert builder != null && number.length() > suffix;  
  572.         
  573.       int len = number.length() - suffix;  
  574.       int qian = len > 3 ? (int)(number.charAt(len-4)-'0') : -1;  
  575.       int bai = len > 2 ? (int)(number.charAt(len-3)-'0') : -1;  
  576.       int shi = len > 1 ? (int)(number.charAt(len-2)-'0') : -1;  
  577.       int ind = (int)(number.charAt(len-1)-'0');  
  578.         
  579.       boolean nonZero = false// true if any of the digits is not zero  
  580.       if( qian == 0 ) {   
  581.         if( bai > 0 || shi > 0 || ind > 0 )   
  582.           builder.append(getConnectDisplay(Connect.Ling));   
  583.       }  
  584.       else if( qian > 0 ){  
  585.         builder.append(getDigitDisplay(Digit.values()[qian]))  
  586.                .append(getConnectDisplay(Connect.Qian));  
  587.         nonZero = true;  
  588.       }  
  589.         
  590.       if( bai == 0 ) {   
  591.         if( qian > 0 && (shi > 0 || ind > 0) )   
  592.           builder.append(getConnectDisplay(Connect.Ling));   
  593.       }  
  594.       else if( bai > 0 ){  
  595.         builder.append(getDigitDisplay(Digit.values()[bai]))  
  596.                .append(getConnectDisplay(Connect.Bai));  
  597.         nonZero = true;  
  598.       }  
  599.         
  600.       if( shi == 0 ) {   
  601.         if( bai > 0 && ind > 0 )   
  602.           builder.append(getConnectDisplay(Connect.Ling));   
  603.       }  
  604.       else if( shi > 0 ){  
  605.         if( number.length() > 2 || shi != 1 )  
  606.           builder.append(getDigitDisplay(Digit.values()[shi]));  
  607.         builder.append(getConnectDisplay(Connect.Shi));  
  608.         nonZero = true;  
  609.       }  
  610.         
  611.       if( ind == 0 ){  
  612.         boolean addZero = len == 1;  
  613.         for(int i=1; addZero && i<=suffix; i++) {  
  614.           if( number.charAt(i) != '0' )  
  615.             addZero = false;  
  616.         }  
  617.         if( addZero ) builder.append(getConnectDisplay(Connect.Ling));  
  618.       }  
  619.       else {  
  620.         builder.append(getDigitDisplay(Digit.values()[ind]));  
  621.         nonZero = true;  
  622.       }  
  623.       return nonZero;  
  624.     }  
  625.       
  626.     String getConnectDisplay(Connect connect) {  
  627.         
  628.       assert connect != null;  
  629.         
  630.       return type == Type.Simplified ?   
  631.              connect.display :   
  632.              connect.displayTraditional;  
  633.     }  
  634.       
  635.     String getPowerDisplay(Power power) {  
  636.         
  637.       assert power != null;  
  638.         
  639.       return type == Type.Simplified ?   
  640.              power.display :   
  641.              power.displayTraditional;  
  642.     }  
  643.       
  644.     String getDigitDisplay(Digit digit) {  
  645.         
  646.       assert digit != null;  
  647.         
  648.       return type == Type.Simplified ?   
  649.              digit.display :   
  650.              digit.displayTraditional;  
  651.     }  
  652.   }  
  653. }  

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多