.Net/C#/VB/T-SQL/Java/Script 实现: 将天文数字转换成中文大写 (2000 年前的思路,打劫的,一点儿技术含量都没有)...

C# Code:

ExpandedBlockStart.gif ContractedBlock.gif /**/ /*
InBlock.gif.Net/C#/T-SQL/VB/Java 实现: 将天文数字转换成中文大写
InBlock.gif最近这里流行这个大写金额转换
InBlock.gif我看了几个,感觉思路都不如这个 2000 年左右用 VB6 写的:
InBlock.gif《VB6 之数据格式化对象使用技巧》
InBlock.gif
http://search.csdn.net/Expert/topic/38/38970.xml?temp=.5078089
InBlock.gif<<精华: 将金额小写转大写的代码可转到亿位>>
InBlock.gif
http://www.dev-club.com/club/bbs/showEssence.asp?id=20684&page=1
InBlock.gif思路非常简单,且没有任何位数限制!
InBlock.gif例如: 401,0103,1013
InBlock.gif读作: 肆佰零壹[亿]零壹佰零叁[万]壹仟零壹拾叁
InBlock.gif咱们先按每四位一组 从左到右,高位到低位分别"大声朗读"一下:
InBlock.gif"肆佰零壹" 单位是: "[亿]"
InBlock.gif"壹佰零叁" 单位是: "[万]"
InBlock.gif"壹仟零壹拾叁" 单位是 "" (相当于没有单位)
InBlock.gif很容易发现,每四位: 只有 千位,百位,十位,个位 这四种情况!
InBlock.gif我们把 [万],[亿] 当作单位就可以了!
InBlock.gif这就是规律了!简单吧!
InBlock.gif依据该思路,只用区区不到 50 行代码就可以搞定:
InBlock.gif只要你能够提供足够多的"单位"
InBlock.gif任何天文数字都可以正确转换!
ExpandedBlockEnd.gif
*/

None.gif
None.gif
namespace  Microshaoft
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif    
using System;
InBlock.gif
InBlock.gif    
public class Util
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
public static string ConvertNumberToChinese(string x, string[] Nums, string[] Digits, string[] Units)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
string S = ""//返回值
InBlock.gif
            int p = 0//字符位置指针
InBlock.gif
            int m = x.Length % 4//取模
InBlock.gif
InBlock.gif            
// 四位一组得到组数
InBlock.gif
            int k = (m > 0 ? x.Length / 4 + 1 : x.Length / 4);
InBlock.gif
InBlock.gif            
// 外层循环在所有组中循环
InBlock.gif            
// 从左到右 高位到低位 四位一组 逐组处理
InBlock.gif            
// 每组最后加上一个单位: "[万亿]","[亿]","[万]"
InBlock.gif
            for (int i = k; i > 0; i--)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
int L = 4;
InBlock.gif                
if (i == k && m != 0)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    L 
= m;
ExpandedSubBlockEnd.gif                }

InBlock.gif                
// 得到一组四位数 最高位组有可能不足四位
InBlock.gif
                string s = x.Substring(p, L);
InBlock.gif                
int l = s.Length;
InBlock.gif
InBlock.gif                
// 内层循环在该组中的每一位数上循环 从左到右 高位到低位
InBlock.gif
                for (int j = 0; j < l; j++)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    
//处理改组中的每一位数加上所在位: "仟","佰","拾",""(个)
InBlock.gif
                    int n = Convert.ToInt32(s.Substring(j, 1));
InBlock.gif                    
if (n == 0)
ExpandedSubBlockStart.gifContractedSubBlock.gif                    
dot.gif{
InBlock.gif                        
if (j < l - 1
InBlock.gif                            
&& Convert.ToInt32(s.Substring(j + 11)) > 0 //后一位(右低)
InBlock.gif
                            && !S.EndsWith(Nums[n]))
ExpandedSubBlockStart.gifContractedSubBlock.gif                        
dot.gif{
InBlock.gif                            S 
+= Nums[n];
ExpandedSubBlockEnd.gif                        }

ExpandedSubBlockEnd.gif                    }

InBlock.gif                    
else
ExpandedSubBlockStart.gifContractedSubBlock.gif                    
dot.gif{
InBlock.gif                        
//处理 1013 一千零"十三", 1113 一千一百"一十三"
InBlock.gif
                        if (!(n == 1 && (S.EndsWith(Nums[0]) | S.Length == 0&& j == l - 2))
ExpandedSubBlockStart.gifContractedSubBlock.gif                        
dot.gif{
InBlock.gif                            S 
+= Nums[n];
ExpandedSubBlockEnd.gif                        }

InBlock.gif                        S 
+= Digits[l - j - 1];
ExpandedSubBlockEnd.gif                    }

ExpandedSubBlockEnd.gif                }

InBlock.gif                p 
+= L;
InBlock.gif                
// 每组最后加上一个单位: [万],[亿] 等
InBlock.gif
                if (i < k) //不是最高位的一组
ExpandedSubBlockStart.gifContractedSubBlock.gif
                dot.gif{
InBlock.gif                    
if (Convert.ToInt32(s) != 0)
ExpandedSubBlockStart.gifContractedSubBlock.gif                    
dot.gif{
InBlock.gif                        
//如果所有 4 位不全是 0 则加上单位 [万],[亿] 等
InBlock.gif
                        S += Units[i - 1];
ExpandedSubBlockEnd.gif                    }

ExpandedSubBlockEnd.gif                }

InBlock.gif                
else
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    
//处理最高位的一组,最后必须加上单位
InBlock.gif
                    S += Units[i - 1];
ExpandedSubBlockEnd.gif                }

ExpandedSubBlockEnd.gif            }

InBlock.gif            
return S;
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
// 测试程序
InBlock.gif
        private static void Main()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
//数字 数组
ExpandedSubBlockStart.gifContractedSubBlock.gif
            string[] Nums = new string[] dot.gif{""""""""""""""""""""};
InBlock.gif            
//位 数组
ExpandedSubBlockStart.gifContractedSubBlock.gif
            string[] Digits = new string[] dot.gif{""""""""};
InBlock.gif            
//单位 数组
ExpandedSubBlockStart.gifContractedSubBlock.gif
            string[] Units = new string[] dot.gif{"""[万]""[亿]""[万亿]"};
InBlock.gif
InBlock.gif            Console.WriteLine(ConvertNumberToChinese(
"1100000013", Nums, Digits, Units));
InBlock.gif            Console.WriteLine(ConvertNumberToChinese(
"2100000000", Nums, Digits, Units));
InBlock.gif            Console.WriteLine(ConvertNumberToChinese(
"1000000000", Nums, Digits, Units));
InBlock.gif            Console.WriteLine(ConvertNumberToChinese(
"40000000013", Nums, Digits, Units));
InBlock.gif            Console.WriteLine(ConvertNumberToChinese(
"40000000001", Nums, Digits, Units));
InBlock.gif            Console.WriteLine(ConvertNumberToChinese(
"400000010000", Nums, Digits, Units));
InBlock.gif            Console.WriteLine(ConvertNumberToChinese(
"40101031013", Nums, Digits, Units));
InBlock.gif            Console.WriteLine(ConvertNumberToChinese(
"40101031113", Nums, Digits, Units));
InBlock.gif            Console.WriteLine(ConvertNumberToChinese(
"101140101031013", Nums, Digits, Units));
InBlock.gif            Console.WriteLine(ConvertNumberToChinese(
"100000001000013", Nums, Digits, Units));
InBlock.gif            Console.WriteLine(ConvertNumberToChinese(
"100000001000113", Nums, Digits, Units));
InBlock.gif            Console.WriteLine(ConvertNumberToChinese(
"100011003", Nums, Digits, Units));
InBlock.gif            Console.WriteLine(ConvertNumberToChinese(
"10010103", Nums, Digits, Units));
InBlock.gif            Console.WriteLine(ConvertNumberToChinese(
"10110013", Nums, Digits, Units));
InBlock.gif            Console.WriteLine(ConvertNumberToChinese(
"130000", Nums, Digits, Units));
InBlock.gif            Console.ReadLine();
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}


2005-04-26 参阅
aierong
自定义格式字符串随笔
顺便贴一个转大写的 Formatter

None.gif namespace  Microshaoft
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif    
using System;
InBlock.gif
InBlock.gif    
public class ChineseFormat : System.ICustomFormatter, System.IFormatProvider
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
//如果format Type与当前实例类型相同,则为当前实例,否则为空引用 
InBlock.gif
        public object GetFormat(Type format)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
if (format == typeof (ICustomFormatter))
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
return this;
ExpandedSubBlockEnd.gif            }

InBlock.gif            
return null;
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
//实现Format方法说明: 
InBlock.gif        
//如果您的格式方法不支持格式,则确定正在设置格式的对象是否实现 IFormattable 接口。 
InBlock.gif        
//如果实现,请调用该接口的IFormattable.ToString 方法。 
InBlock.gif        
//否则,调用基础对象的默认 Object.ToString 方法。 
InBlock.gif
        public string Format(string format, object arg, IFormatProvider provider)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
if (format == null)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
if (arg is IFormattable)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    
return ((IFormattable) arg).ToString(format, provider);
ExpandedSubBlockEnd.gif                }

InBlock.gif                
return arg.ToString();
ExpandedSubBlockEnd.gif            }

InBlock.gif            
else
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
if (format == "ChineseFormat")
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif                    
string[] Nums = new string[] dot.gif{""""""""""""""""""""};
InBlock.gif                    
//位 数组 
ExpandedSubBlockStart.gifContractedSubBlock.gif
                    string[] Digits = new string[] dot.gif{""""""""};
InBlock.gif                    
//单位 数组 
ExpandedSubBlockStart.gifContractedSubBlock.gif
                    string[] Units = new string[] dot.gif{"""[万]""[亿]""[万亿]"};
InBlock.gif                    
return ConvertNumberToChinese(arg.ToString(), Nums, Digits, Units);
InBlock.gif                    
//return "***"+arg.ToString(); 
ExpandedSubBlockEnd.gif
                }

InBlock.gif                
else
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    
if (arg is IFormattable)
ExpandedSubBlockStart.gifContractedSubBlock.gif                    
dot.gif{
InBlock.gif                        
return ((IFormattable) arg).ToString(format, provider);
ExpandedSubBlockEnd.gif                    }

InBlock.gif                    
return arg.ToString();
ExpandedSubBlockEnd.gif                }

ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
public static string ConvertNumberToChinese(string x, string[] Nums, string[] Digits, string[] Units)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
string S = ""//返回值 
InBlock.gif
            int p = 0//字符位置指针 
InBlock.gif
            int m = x.Length % 4//取模 
InBlock.gif
InBlock.gif            
// 四位一组得到组数 
InBlock.gif
            int k = (m > 0 ? x.Length / 4 + 1 : x.Length / 4);
InBlock.gif
InBlock.gif            
// 外层循环在所有组中循环 
InBlock.gif            
// 从左到右 高位到低位 四位一组 逐组处理 
InBlock.gif            
// 每组最后加上一个单位: "[万亿]","[亿]","[万]" 
InBlock.gif
            for (int i = k; i > 0; i--)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
int L = 4;
InBlock.gif                
if (i == k && m != 0)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    L 
= m;
ExpandedSubBlockEnd.gif                }

InBlock.gif                
// 得到一组四位数 最高位组有可能不足四位 
InBlock.gif
                string s = x.Substring(p, L);
InBlock.gif                
int l = s.Length;
InBlock.gif
InBlock.gif                
// 内层循环在该组中的每一位数上循环 从左到右 高位到低位 
InBlock.gif
                for (int j = 0; j < l; j++)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    
//处理改组中的每一位数加上所在位: "仟","佰","拾",""(个) 
InBlock.gif
                    int n = Convert.ToInt32(s.Substring(j, 1));
InBlock.gif                    
if (n == 0)
ExpandedSubBlockStart.gifContractedSubBlock.gif                    
dot.gif{
InBlock.gif                        
if (j < l - 1
InBlock.gif                            
&& Convert.ToInt32(s.Substring(j + 11)) > 0 //后一位(右低) 
InBlock.gif
                            && !S.EndsWith(Nums[n]))
ExpandedSubBlockStart.gifContractedSubBlock.gif                        
dot.gif{
InBlock.gif                            S 
+= Nums[n];
ExpandedSubBlockEnd.gif                        }

ExpandedSubBlockEnd.gif                    }

InBlock.gif                    
else
ExpandedSubBlockStart.gifContractedSubBlock.gif                    
dot.gif{
InBlock.gif                        
//处理 1013 一千零"十三", 1113 一千一百"一十三" 
InBlock.gif
                        if (!(n == 1 && (S.EndsWith(Nums[0]) | S.Length == 0&& j == l - 2))
ExpandedSubBlockStart.gifContractedSubBlock.gif                        
dot.gif{
InBlock.gif                            S 
+= Nums[n];
ExpandedSubBlockEnd.gif                        }

InBlock.gif                        S 
+= Digits[l - j - 1];
ExpandedSubBlockEnd.gif                    }

ExpandedSubBlockEnd.gif                }

InBlock.gif                p 
+= L;
InBlock.gif                
// 每组最后加上一个单位: [万],[亿] 等 
InBlock.gif
                if (i < k) //不是最高位的一组 
ExpandedSubBlockStart.gifContractedSubBlock.gif
                dot.gif{
InBlock.gif                    
if (Convert.ToInt32(s) != 0)
ExpandedSubBlockStart.gifContractedSubBlock.gif                    
dot.gif{
InBlock.gif                        
//如果所有 4 位不全是 0 则加上单位 [万],[亿] 等 
InBlock.gif
                        S += Units[i - 1];
ExpandedSubBlockEnd.gif                    }

ExpandedSubBlockEnd.gif                }

InBlock.gif                
else
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    
//处理最高位的一组,最后必须加上单位 
InBlock.gif
                    S += Units[i - 1];
ExpandedSubBlockEnd.gif                }

ExpandedSubBlockEnd.gif            }

InBlock.gif            
return S;
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}

None.gif
None.gif
namespace  Test
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif    
using System;
InBlock.gif    
using Microshaoft;
InBlock.gif
InBlock.gif    
class AppTest
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
static void Main()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
string printString = String.Empty;
InBlock.gif            
long i = 1100000013;
InBlock.gif            ChineseFormat fmt 
= new ChineseFormat();
InBlock.gif
InBlock.gif            printString 
= string.Format(fmt, "显示正常格式: {0}", i);
InBlock.gif            Console.WriteLine(printString);
InBlock.gif            printString 
= string.Format(fmt, "显示正常格式: {0:C}", i);
InBlock.gif            Console.WriteLine(printString);
InBlock.gif            printString 
= string.Format(fmt, "显示自定义格式: {0:ChineseFormat}", i);
InBlock.gif            Console.WriteLine(printString);
InBlock.gif
InBlock.gif            Console.ReadLine();
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}

None.gif


T-SQL UDF Code:

None.gif alter   function  ConvertNumberToChinese(@  varchar ( 100 ))
None.gif
returns   varchar ( 100 )
None.gif
as
None.gif
begin
None.gif
-- declare @ varchar(100)
None.gif--
set @ = '101140101031013'
None.gif
declare  @s  varchar ( 100 )
None.gif
set  @s  =   ''
None.gif
None.gif
declare  @p  integer
None.gif
set  @p  =   0
None.gif
None.gif
declare  @m  integer
None.gif
set  @m  =   len (@)  %   4
None.gif
None.gif
declare  @k  integer
None.gif
set  @k  =   len (@) / 4
None.gif
None.gif
select  @k  =  @k  +   1
None.gif 
where  @m  >   0
None.gif
None.gif
declare  @i  integer
None.gif
set  @i  =  @k
None.gif
None.gif
while  (@i  >   0 )
None.gif
begin   -- outer
None.gif
    declare  @L  integer
None.gif   
set  @L  =   4
None.gif
None.gif   
select  @L  =  @m
None.gif    
where  @i  =  @k  and  @m  !=   0
None.gif
None.gif   
declare  @ss  varchar ( 4 )
None.gif   
set  @ss  =   substring (@,@p + 1 ,@L)
None.gif
None.gif   
declare  @ll  integer
None.gif   
set  @ll  =   len (@ss)
None.gif
None.gif   
-- inner
None.gif
    declare  @j  integer
None.gif   
set  @j  =   0
None.gif
None.gif   
while  (@j  <  @ll)  -- inner
None.gif
    begin   -- inner
None.gif
       declare  @n  integer
None.gif      
set  @n  =   cast ( substring (@ss,@j + 1 , 1 as   integer )
None.gif
None.gif      
declare  @num  varchar ( 2 )   
None.gif      
select  @num  =  Num
None.gif        
from  (
None.gif              
select   0   as  id, ' '   as  Num
None.gif              
union   all   select   1 , ' '
None.gif              
union   all   select   2 , ' '
None.gif              
union   all   select   3 , ' '
None.gif              
union   all   select   4 , ' '
None.gif              
union   all   select   5 , ' '
None.gif              
union   all   select   6 , ' '
None.gif              
union   all   select   7 , ' '
None.gif              
union   all   select   8 , ' '
None.gif              
union   all   select   9 , ' '
None.gif             ) Nums
None.gif      
where  id  =  @n
None.gif
None.gif      
if  @n  =   0
None.gif      
begin
None.gif         
select  @s  =  @s  +  @num
None.gif          
where  @j  <  @ll  -   1
None.gif                
and   cast ( substring (@ss,(@j + 1 ) + 1 , 1 as   integer >   0
None.gif                
and   right (@ss, 1 !=  @num
None.gif      
end
None.gif      
else
None.gif      
begin
None.gif         
declare  @jj  integer
None.gif         
set  @jj  =   1
None.gif
None.gif         
select  @jj  =  @j  -   1
None.gif          
where  @j  >   1
None.gif
None.gif         
select  @s  =  @s  +  @num
None.gif          
where   not  (@n  =   1
None.gif                     
and  @j  =  @ll  -   2
None.gif                     
and  ( len (@s)  =   0
None.gif                          
or   right (@s, 1 =   ' '
ExpandedBlockStart.gifContractedBlock.gif
/**/ /*
InBlock.gif                                           (   select Num
InBlock.gif                                                from
InBlock.gif                                               (
InBlock.gif                                                select 0 as id,'零' as Num
InBlock.gif                                                union all select 1,'壹'
InBlock.gif                                                union all select 2,'贰'
InBlock.gif                                                union all select 3,'叁'
InBlock.gif                                                union all select 4,'肆'
InBlock.gif                                                union all select 5,'伍'
InBlock.gif                                                union all select 6,'陆'
InBlock.gif                                                union all select 7,'柒'
InBlock.gif                                                union all select 8,'捌'
InBlock.gif                                                union all select 9,'玖'
InBlock.gif                                               ) Nums
InBlock.gif                                               where id = 0
InBlock.gif                                           )
ExpandedBlockEnd.gif
*/

None.gif                                           )
None.gif                         )
None.gif
None.gif         
select  @s  =  @s  +  digit
None.gif           
from  (
None.gif                 
select   0   as  id, ''   as  digit
None.gif                 
union   all   select   1 , ' '
None.gif                 
union   all   select   2 , ' '
None.gif                 
union   all   select   3 , ' '
None.gif                ) digits
None.gif          
where  id  =  @ll  -  @j  -   1
None.gif
None.gif      
end
None.gif      
set  @j  =  @j  +   1   -- inner
None.gif
    end   -- inner
None.gif
    set  @p  =  @p  +  @L
None.gif
None.gif   
declare  @unit  varchar ( 10 )
None.gif   
select  @unit  =  Unit
None.gif     
from  (
None.gif           
select   0   as  id, ''   as  Unit
None.gif           
union   all   select   1 , ' [万] '
None.gif           
union   all   select   2 , ' [亿] '
None.gif           
union   all   select   3 , ' [万亿] '
None.gif          ) Units
None.gif    
where  id  =  @i  -   1
None.gif
None.gif   
if  @i  <  @k
None.gif   
begin
None.gif      
select  @s  =  @s  +  @unit
None.gif       
where   cast (@ss  as   integer !=   0
None.gif   
end
None.gif   
else
None.gif   
begin
None.gif      
set  @s  =  @s  +  @unit
None.gif   
end
None.gif   
set  @i  =  @i  -   1   --  outer
None.gif
end   -- out
None.gif
return  @s
ExpandedBlockStart.gifContractedBlock.gif
/**/ /*
InBlock.gif--Test:
InBlock.gifselect dbo.ConvertNumberToChinese('1011111112101013')
InBlock.gif,dbo.ConvertNumberToChinese('40000000001')
InBlock.gif,dbo.ConvertNumberToChinese('400000010000')
InBlock.gif,dbo.ConvertNumberToChinese('40101031013')
InBlock.gif,dbo.ConvertNumberToChinese('101140101031013')
InBlock.gif,dbo.ConvertNumberToChinese('100000001000003')
InBlock.gif,dbo.ConvertNumberToChinese('10011003')
InBlock.gif,dbo.ConvertNumberToChinese('10010103')
InBlock.gif,dbo.ConvertNumberToChinese('10010013')
ExpandedBlockEnd.gif
*/

None.gif
end



VB6 Code:
《VB6 之数据格式化对象使用技巧》
http://search.csdn.net/Expert/topic/38/38970.xml?temp=.5078089
<<精华: 将金额小写转大写的代码可转到亿位>>
http://www.dev-club.com/club/bbs/showEssence.asp?id=20684&page=1

VB.Net Code:
可自行将 C# Code 编译成 EXE 后, Reflector 反编译获取 VB.Net/Delphi.Net/IL 等语言源码

Java Code:

None.gif public   class  Class1
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif    
public static String ConvertNumberToChinese(String x, String[] Nums, String[] Digits, String[] Units)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        String S 
= ""//返回值
InBlock.gif
        int p = 0//字符位置指针
InBlock.gif
        int m = x.length() % 4//取模
InBlock.gif
InBlock.gif        
// 四位一组得到组数
InBlock.gif
        int k = (m > 0 ? x.length() / 4 + 1 : x.length() / 4);
InBlock.gif
InBlock.gif        
// 外层循环在所有组中循环
InBlock.gif        
// 从左到右 高位到低位 四位一组 逐组处理
InBlock.gif        
// 每组最后加上一个单位: "[万亿]","[亿]","[万]"
InBlock.gif
        for (int i = k; i > 0; i--)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
int L = 4;
InBlock.gif            
if (i == k && m != 0)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                L 
= m;
ExpandedSubBlockEnd.gif            }

InBlock.gif            
// 得到一组四位数 最高位组有可能不足四位
InBlock.gif
            String s = x.substring(p, p + L);
InBlock.gif            
int l = s.length();
InBlock.gif
InBlock.gif            
// 内层循环在该组中的每一位数上循环 从左到右 高位到低位
InBlock.gif
            for (int j = 0; j < l; j++)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
//处理改组中的每一位数加上所在位: "仟","佰","拾",""(个)
InBlock.gif
                int n = java.lang.Integer.parseInt(s.substring(j, j+1));
InBlock.gif                
if (n == 0)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    
if ((j < l - 1)
InBlock.gif                        
&& (java.lang.Integer.parseInt(s.substring(j + 1, j + 1+ 1)) > 0//后一位(右低)
InBlock.gif
                        && !S.endsWith(Nums[n]))
ExpandedSubBlockStart.gifContractedSubBlock.gif                    
dot.gif{
InBlock.gif                        S 
+= Nums[n];
ExpandedSubBlockEnd.gif                    }

ExpandedSubBlockEnd.gif                }

InBlock.gif                
else
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    
//处理 1013 一千零"十三", 1113 一千一百"一十三"
InBlock.gif
                    if (!(n == 1 && (S.endsWith(Nums[0]) | S.length() == 0&& j == l - 2))
ExpandedSubBlockStart.gifContractedSubBlock.gif                    
dot.gif{
InBlock.gif                        S 
+= Nums[n];
ExpandedSubBlockEnd.gif                    }

InBlock.gif                    S 
+=  Digits[l - j - 1];
ExpandedSubBlockEnd.gif                }

ExpandedSubBlockEnd.gif            }

InBlock.gif            p 
+= L;
InBlock.gif            
// 每组最后加上一个单位: [万],[亿] 等
InBlock.gif
            if (i < k) //不是最高位的一组
ExpandedSubBlockStart.gifContractedSubBlock.gif
            dot.gif{
InBlock.gif                
if (java.lang.Integer.parseInt(s) != 0)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    
//如果所有 4 位不全是 0 则加上单位 [万],[亿] 等
InBlock.gif
                    S += Units[i - 1];
ExpandedSubBlockEnd.gif                }

ExpandedSubBlockEnd.gif            }

InBlock.gif            
else
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
//处理最高位的一组,最后必须加上单位
InBlock.gif
                S += Units[i - 1];
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif        
return S;
ExpandedSubBlockEnd.gif    }

InBlock.gif
InBlock.gif    
// 测试程序
InBlock.gif
    public static void main(String[] args) throws Exception
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
//数字 数组
ExpandedSubBlockStart.gifContractedSubBlock.gif
        String[] Nums = new String[] dot.gif{""""""""""""""""""""};
InBlock.gif        
//位 数组
ExpandedSubBlockStart.gifContractedSubBlock.gif
        String[] Digits = new String[] dot.gif{""""""""};
InBlock.gif        
//单位 数组
ExpandedSubBlockStart.gifContractedSubBlock.gif
        String[] Units = new String[] dot.gif{"""[万]""[亿]""[万亿]"};
InBlock.gif
InBlock.gif        System.
out.println(ConvertNumberToChinese("111112100113", Nums, Digits, Units));
InBlock.gif        System.
out.println(ConvertNumberToChinese("1100000000", Nums, Digits, Units));
InBlock.gif        System.
out.println(ConvertNumberToChinese("1000000000", Nums, Digits, Units));
InBlock.gif        System.
out.println(ConvertNumberToChinese("40000000013", Nums, Digits, Units));
InBlock.gif        System.
out.println(ConvertNumberToChinese("40000000001", Nums, Digits, Units));
InBlock.gif        System.
out.println(ConvertNumberToChinese("400000010000", Nums, Digits, Units));
InBlock.gif        System.
out.println(ConvertNumberToChinese("40101031013", Nums, Digits, Units));
InBlock.gif        System.
out.println(ConvertNumberToChinese("40101031113", Nums, Digits, Units));
InBlock.gif        System.
out.println(ConvertNumberToChinese("101140101031013", Nums, Digits, Units));
InBlock.gif        System.
out.println(ConvertNumberToChinese("100000001000013", Nums, Digits, Units));
InBlock.gif        System.
out.println(ConvertNumberToChinese("100000001000113", Nums, Digits, Units));
InBlock.gif        System.
out.println(ConvertNumberToChinese("100011003", Nums, Digits, Units));
InBlock.gif        System.
out.println(ConvertNumberToChinese("10010103", Nums, Digits, Units));
InBlock.gif        System.
out.println(ConvertNumberToChinese("10110013", Nums, Digits, Units));
InBlock.gif        System.
out.println(ConvertNumberToChinese("130000", Nums, Digits, Units));
InBlock.gif
InBlock.gif        
//System.in.read();
ExpandedSubBlockEnd.gif
    }

ExpandedBlockEnd.gif}

None.gif

JavaScript:

None.gif <! DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
None.gif
< HTML >
None.gif
< HEAD >
None.gif
< TITLE >  New Document  </ TITLE >
None.gif
< META  NAME ="Generator"  CONTENT ="EditPlus" >
None.gif
< META  NAME ="Author"  CONTENT ="playyuer㊣www.Microshaoft.com" >
None.gif
</ HEAD >
None.gif
None.gif
< BODY >
ExpandedBlockStart.gifContractedBlock.gif
< SCRIPT  LANGUAGE ="JavaScript" > dot.gif
InBlock.gif
<!--
InBlock.gif
function ConvertNumberToChinese(x,Nums,Digits,Units)
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gif    
//var x = "11112100013";
InBlock.gif
    //
InBlock.gif
    //var Nums = new Array("零", "壹", "贰", "叁", "肆", "伍", "陆", "柒", "捌", "玖"); 
InBlock.gif
    //var Digits = new Array("", "拾", "佰", "仟"); 
InBlock.gif
    //var Units = new Array("", "[万]", "[亿]", "[万亿]"); 
InBlock.gif

InBlock.gif    
var S = ""//返回值 
InBlock.gif
    var p = 0//字符位置指针 
InBlock.gif
    var m = x.length % 4//取模 
InBlock.gif

InBlock.gif    
// 四位一组得到组数 
InBlock.gif
    var k = (m > 0 ? Math.floor(x.length / 4+ 1 : Math.floor(x.length / 4)); 
InBlock.gif    
// 外层循环在所有组中循环 
InBlock.gif
    // 从左到右 高位到低位 四位一组 逐组处理 
InBlock.gif
    // 每组最后加上一个单位: "[万亿]","[亿]","[万]" 
InBlock.gif
    for (var i = k; i > 0; i--
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
var L = 4
InBlock.gif        
if (i == k && m != 0)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            L 
= m;
ExpandedSubBlockEnd.gif        }

InBlock.gif        
// 得到一组四位数 最高位组有可能不足四位 
InBlock.gif
        var s = x.substring(p, p + L);
InBlock.gif        
var l = s.length;
InBlock.gif
InBlock.gif        
// 内层循环在该组中的每一位数上循环 从左到右 高位到低位 
InBlock.gif
        for (var j = 0;j < l;j++)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
var n = parseInt(s.substring(j, j+1));
InBlock.gif            
if (n == 0)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
if ((j < l - 1&& (parseInt(s.substring(j + 1, j + 1+ 1)) > 0//后一位(右低) 
InBlock.gif
                    && S.substring(S.length-1,S.length) != Nums[n])
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    S 
+= Nums[n];
ExpandedSubBlockEnd.gif                }

ExpandedSubBlockEnd.gif            }

InBlock.gif            
else 
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
//处理 1013 一千零"十三", 1113 一千一百"一十三" 
InBlock.gif
                if (!(n == 1 && (S.substring(S.length-1,S.length) == Nums[0| S.length == 0&& j == l - 2)) 
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    S 
+= Nums[n];
ExpandedSubBlockEnd.gif                }

InBlock.gif                S 
+=  Digits[l - j - 1];
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif        p 
+= L;
InBlock.gif        
// 每组最后加上一个单位: [万],[亿] 等 
InBlock.gif
        if (i < k)//不是最高位的一组 
ExpandedSubBlockStart.gifContractedSubBlock.gif
        dot.gif{
InBlock.gif            
if (parseInt(s)!= 0)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
//如果所有 4 位不全是 0 则加上单位 [万],[亿] 等 
InBlock.gif
                S += Units[i - 1];
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif        
else
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
//处理最高位的一组,最后必须加上单位 
InBlock.gif
            S += Units[i - 1];
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif    }

InBlock.gif    
//alert(S);
InBlock.gif
    return S
ExpandedSubBlockEnd.gif}

InBlock.gif
// call function
InBlock.gif
var xx = "11112100013";
InBlock.gif
var xNums = new Array(""""""""""""""""""""); 
InBlock.gif
var xDigits = new Array(""""""""); 
InBlock.gif
var xUnits = new Array("""[万]""[亿]""[万亿]");
InBlock.gifalert(ConvertNumberToChinese(xx,xNums,xDigits,xUnits));
InBlock.gif
ExpandedBlockEnd.gif
//-->
None.gif
</ SCRIPT >
None.gif
</ BODY >
None.gif
</ HTML >
None.gif


 

转载于:https://www.cnblogs.com/Microshaoft/archive/2005/04/02/131008.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值