欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

使用plsql实现阿拉伯数字转中文大写

程序员文章站 2022-03-02 15:48:49
...

使用pl sql的数组功能、循环机制将小于10的16次方的任意阿拉伯数字转换为中文大写数字。该函数从原来的200多行,优化成现在70行左右。

始终相信,最少的代码实现某个功能才是最优雅的开发方式,代码越少出bug的概率就越低。

从开发、调试、到优化这个函数,我用掉了约5个小时的时间,最后只有短短的70行。顺便膜拜一下,那个解放军美女尖兵:90昼夜主持编写40万行代码

 

 

(miki西游 @mikixiyou 原文链接: http://mikixiyou.iteye.com/blog/1608834 )

 

附上函数代码:

create or replace function chinese_number_program(p_input float)
  return varchar2 as
  /*
  created by miki西游 on 2012-7-27
  It is used to change Arabic numbers to Chinese big numbers.
  */
  type typ_money is table of varchar2(20);
  c_numbers typ_money := typ_money('零', '壹', '贰', '叁', '肆', '伍', '陆', '柒', '捌', '玖');
	c_unit typ_money := typ_money('分', '角', '圆', '拾', '佰', '仟', '万', '拾', '佰', '仟', '亿', '拾', '佰', '仟', '兆', '拾', '佰', '仟');
  v_array_money    typ_money := typ_money();
  chinese_number   varchar2(30);
  result           varchar2(1000);
  v_number         integer;
  v_number_element integer;
  i                int;
begin
  if (p_input >= power(10, 16)) then
    result := '超出计算范围';
    return result;
  end if;
  v_number := round(p_input * 100);

  i := 1;
  v_array_money.extend(1);
  v_array_money(v_array_money.count) := '整';

  while v_number > 0 loop
    v_number_element := mod(v_number, 10);
    if (v_number_element = 0) then
      if i in (3, 7, 11, 15) then
        chinese_number := c_unit(i);
      else
        chinese_number := c_numbers(v_number_element + 1);
      end if;
    else
      chinese_number := c_numbers(v_number_element + 1) || c_unit(i);
    end if;
  
    case
      when chinese_number = '零' then
        if (v_array_money(v_array_money.count) not in
           ('整', '零', '圆', '万', '亿', '兆')) then
          v_array_money.extend(1);
          v_array_money(v_array_money.count) := chinese_number;
        end if;
      when chinese_number like '%亿' then
        if (v_array_money(v_array_money.count) in ('万')) then
          v_array_money(v_array_money.count) := chinese_number;
        else
          v_array_money.extend(1);
          v_array_money(v_array_money.count) := chinese_number;
        end if;
      when chinese_number like '%兆' then
        if (v_array_money(v_array_money.count) in ('万', '亿')) then
          v_array_money(v_array_money.count) := chinese_number;
        else
          v_array_money.extend(1);
          v_array_money(v_array_money.count) := chinese_number;
        end if;
      else
        v_array_money.extend(1);
        v_array_money(v_array_money.count) := chinese_number;
    end case;
    v_number := floor(v_number / 10);
    i        := i + 1;
  end loop;
  for i in v_array_money.first .. v_array_money.last loop
    result := v_array_money(i) || result;
  end loop;

  return result;
end;

 

测试结果如下:

SQL> select chinese_number_program(9999999999999999.99) from dual;\
 
CHINESE_NUMBER_PROGRAM(9999999
--------------------------------------------------------------------------------
玖仟玖佰玖拾玖兆玖仟玖佰玖拾玖亿玖仟玖佰玖拾玖万玖仟玖佰玖拾玖圆玖角玖分整
 
SQL> select chinese_number_program(9999999999999999.99) as cc from dual;
 
CC
--------------------------------------------------------------------------------
玖仟玖佰玖拾玖兆玖仟玖佰玖拾玖亿玖仟玖佰玖拾玖万玖仟玖佰玖拾玖圆玖角玖分整
 
SQL> select chinese_number_program(990000001229.00) as cc from dual;
 
CC
--------------------------------------------------------------------------------
玖仟玖佰亿壹仟贰佰贰拾玖圆整
 
SQL> select chinese_number_program(9900029.01) as cc from dual;
 
CC
--------------------------------------------------------------------------------
玖佰玖拾万零贰拾玖圆零壹分整
 
SQL> select chinese_number_program(10029.3) as cc from dual;
 
CC
--------------------------------------------------------------------------------
壹万零贰拾玖圆叁角整
 
SQL> 
 

 

 

btw,如果您觉得有同感,请鼓励一下。呵呵

 

 

objective-c

 

#import <Foundation/Foundation.h>
#import <stdio.h>

void print( NSArray *array ) {
    int i;
    for (i=0;i<[array count];i++)
    {
        NSLog(@"%@",[array objectAtIndex:i]);
    }

}

void print2( NSArray *array ) {
    NSEnumerator *enumerator = [array objectEnumerator];
    id obj;
    while ( obj = [enumerator nextObject] ) {
        printf( "%s\n", [[obj description] cString] );
    }
}

void print3( NSArray *array ) {
    id obj;
    NSLog(@"new array is ");
    for (obj in array)
    {
        NSLog(@"%@",obj);
    }
}

NSString * printByLine( NSArray *array ) {
    id obj;
    NSMutableString * oneLine;
    oneLine=[NSMutableString stringWithCapacity:100];
    NSString * oneString;

    for (obj in array)
    {
        oneString=[obj copy];

        //[oneLine appendString:oneString];
        [oneLine insertString:oneString atIndex:0];

       // NSLog(@"xx= %@,%d,%d",oneLine,oneLine,oneString);
    }

    return oneLine;


}



int main() {
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];

    //NSArray *arr_numbers = [[NSArray alloc] initWithObjects:@"Áã", @"Ò¼", @"·¡", @"Èþ", @"ËÁ", @"Îé", @"½", @"Æâ", @"°Æ", @"¾Á", nil];
    NSArray *arr_numbers = [[NSArray alloc] initWithObjects:@"Zero", @"One", @"Two", @"Three", @"Four", @"Five", @"Six", @"Seven", @"Eight", @"Nine", nil];

    //NSArray *arr_unit = [[NSArray alloc] initWithObjects:@"·Ö", @"½Ç", @"Ô²", @"Ê°", @"°Û", @"Ǫ", @"Íò", @"Ê°", @"°Û", @"Ǫ", @"ÒÚ", @"Ê°", @"°Û", @"Ǫ", @"Õ×", @"Ê°", @"°Û", @"Ǫ",nil];
    NSArray *arr_unit = [[NSArray alloc] initWithObjects:@"F", @"J", @"Yuan", @"S", @"B", @"Q", @"W", @"S", @"B", @"Q", @"Yi", @"S", @"B", @"Q", @"Z", @"S", @"B", @"Q",nil];

    NSMutableArray * mutable_money = [[NSMutableArray alloc] init];

    NSSet * set_special_number=[[NSSet alloc] initWithObjects:@"Zheng",@"Zero",@"Yuan",@"W",@"Yi",@"Z"];

    //print(arr_numbers);
   // print2(arr_numbers);
   // print3(arr_unit);

    float p_input_number;
    int input_number;
    int input_number_element;
    int i=0;

    NSString * chinese_number;
    NSString * result;
    p_input_number=1008.20;

    if (p_input_number>9999999)
    {
        result =@"out of maximize range";
        NSLog(@"%@",result);
        return 0;
    }


    input_number=p_input_number*100;

    [mutable_money addObject:@"Zheng"];

    //[[chinese_number alloc] init];

    while (input_number>0)
    {
        input_number_element=input_number%10;

        if(input_number_element==0)
        {
            if (i == 2 || i==6 || i==10 || i==14)
            {
                chinese_number=[arr_unit objectAtIndex:i];
            }
            else
            {
                chinese_number=[arr_numbers objectAtIndex:input_number_element];
            }

        }
        else
        {
            chinese_number=[[arr_numbers objectAtIndex:input_number_element] stringByAppendingString:[arr_unit objectAtIndex:i]];
        }

      //  NSLog(@"%@,%@,%@",chinese_number,[arr_numbers objectAtIndex:input_number_element],[arr_unit objectAtIndex:i]);
       // NSLog(@"%d,%d,%d",chinese_number,[arr_numbers objectAtIndex:input_number_element],[arr_unit objectAtIndex:i]);


        if ([chinese_number isEqualToString:@"Zero"])
        {
            if (!([set_special_number containsObject:[mutable_money lastObject]]))
            {
            [mutable_money addObject:chinese_number];

            //NSLog(@"zero is %@,%@,%@",chinese_number,[arr_numbers objectAtIndex:input_number_element],[arr_unit objectAtIndex:i]);

            }
        }
        else if ([chinese_number hasSuffix:@"Yi"])
        {
            if ([[mutable_money lastObject] isEqualToString:@"W"])
            {
                [mutable_money removeLastObject];
                [mutable_money addObject:chinese_number];
            }
            else
            {
                [mutable_money addObject:chinese_number];
            }

            //NSLog(@"Yi is %@,%@,%@",chinese_number,[arr_numbers objectAtIndex:input_number_element],[arr_unit objectAtIndex:i]);
        }

        else
        {
            [mutable_money addObject:chinese_number];
            //NSLog(@"Others is %@,%@,%@",chinese_number,[arr_numbers objectAtIndex:input_number_element],[arr_unit objectAtIndex:i]);
        }

        input_number=input_number/10;
        i++;

    }





   NSLog(@"%@",printByLine(mutable_money));



    // free memory

    [arr_numbers release];
    [arr_unit release];
    [mutable_money release];

    [pool release];

    return 0;
}