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
  • 相关阅读:
    TKStudio示例工程LPC1220_GPIO_LED学习
    LIve Writer图片自动水印,自动居中,自动为原始大小的设置方法.
    cmd 修改文件属性
    [原创]Java下X86机,Bytes和Int的转换
    [原创]把","号分隔的字串转化成一列的Table
    [原创]Java实现PKCS7填充的DES加密(修订版)
    利用ADODB.Stream实现 Bytes到String的指定编码的转换
    [原创]利用HttpModuler实现WEB程序同一时间只让一个用户实例登陆(修改)
    [原创]用XMLHttp BinaryWrite,Post GB2312编码的字串
    UTF8ToBytes
  • 原文地址:https://www.cnblogs.com/kking/p/1841842.html
Copyright © 2011-2022 走看看