zoukankan      html  css  js  c++  java
  • Just have fun~

    I read about an intresting problem yesterday: how to convert an arabic to Chinese number? It looks simple but actually very complicated and it dose cost me several hours to solve. Code is not commented (because i'm tired),just have fun~

     

    Convert_to_Chinese
    1 import java.util.*;
    2  import java.lang.*;
    3  public class Chinese
    4 {
    5 static private String[] Num = new String [] {"","","","","","","","","",""};
    6 static private String[] Unit = new String [] {"","","","","","","","","",""};
    7
    8  // String toChinese4(){};
    9
    10
    11 static String fraction2Chinese(double fraction)
    12 {
    13 String s = "";
    14
    15 double f = ((int)(fraction*100))/100.0;
    16
    17
    18 if( f >=1 || f<0 )
    19 return s;
    20 else
    21 {
    22 if(f == 0)
    23 s+=Unit[9];
    24 else
    25 { f *= 100;
    26 int b = (int)f/10;
    27 s += Num[b];
    28 if ( b != 0 )
    29 {
    30 s+=Unit[1];
    31 }
    32
    33 b= (int)f%10;
    34
    35 if ( b != 0 )
    36 {
    37 s+=Num[b];
    38 s+=Unit[0];
    39 }
    40 }
    41 }
    42 return s;
    43 }
    44
    45 static String thousands2Chinese (int thousands , boolean head , String tail)
    46 {
    47 String s = "";
    48 int[] integer = new int[4];
    49 for ( int i = 3 ; i >=0 ; i-- )
    50 {
    51 integer[i] = thousands%10;
    52 thousands/=10;
    53 }
    54 int zerocount=0;
    55 boolean zeroprint;
    56
    57 if(!head)
    58 zeroprint = true;
    59 else
    60 zeroprint = false;
    61 for ( int i = 0 ; i < 4 ; i ++ )
    62 {
    63 if( integer[ i ] == 0)
    64 zerocount ++;
    65 else
    66 {
    67 if( zerocount != 0 )
    68 {
    69 if (zeroprint)
    70 {
    71 s+=Num[0];
    72 }
    73 zerocount = 0;
    74 }
    75
    76 s+=Num[integer[i]];
    77 s+=Unit[6-i];
    78 zeroprint = true;
    79 }
    80 }
    81
    82 if(zerocount<4)
    83 s+=tail;
    84
    85 return s;
    86
    87 }
    88
    89 static String num2Chinese(double number)
    90 {
    91 String s = "";
    92 double num = number/100000000;
    93 boolean head = true;
    94 if( num!= 0 )
    95 {
    96 s+=thousands2Chinese( (int)num , head ,Unit[8]);
    97
    98 head = false;
    99 // System.out.println(num);
    100 }
    101
    102 num = number / 10000;
    103 if( num!= 0 )
    104 {
    105 s+=thousands2Chinese( (int)num , head ,Unit[7]);
    106
    107 head = false;
    108 // System.out.println(num);
    109 }
    110
    111 num = ((long)number)%10000;
    112
    113 if( num!= 0 )
    114 {
    115 s+=thousands2Chinese( (int)num , head ,"");
    116 s+=Unit[2];
    117 head = false;
    118 System.out.println(num);
    119 }
    120
    121 double frac = number-(long)number;
    122 //System.out.println(frac);
    123 s+=fraction2Chinese(frac);
    124 return s;
    125 }
    126 public static void main(String[] args)
    127 {
    128 System.out.println(num2Chinese(99901000000.00));
    129 }
    130 }
    131
  • 相关阅读:
    CentOS6 图形界面'Basic server'条件下的(gnome)安装 .
    2013年 2月 春节期间 CgyWin 安装总结
    网络爬虫比较
    如何将nutch项目加载到MyEclipse中,生成一个web project
    读过的书
    NBearLite 用MsAccess数据库时 Count() 的错误(BUG?)
    测试Windows Live Writer
    VS2005 编辑器配色
    事务处理
    MVC MvcApplication 中在.CS文件(CodeBehide)中找不到服务器控件的解决办法
  • 原文地址:https://www.cnblogs.com/kking/p/1841842.html
Copyright © 2011-2022 走看看