码迷,mamicode.com
首页 > 其他好文 > 详细

数字金额大小写转换

时间:2015-06-16 14:41:15      阅读:224      评论:0      收藏:0      [点我收藏+]

标签:

有些时候,需要打印的凭证实现资本额多次,例如

技术分享

有多种写法来实现

比如一下几种

CREATE OR REPLACE FUNCTION chinese_number_program(p_input FLOAT)
   RETURN VARCHAR2 AS

   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;

#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;
}



FUNCTION money_format_zhs(p_money_amount IN NUMBER) RETURN VARCHAR2 IS
  
    c_money_amount VARCHAR2(20);
    n_string       VARCHAR2(40) := '壹贰叁肆伍陆柒捌玖';
    l_string       VARCHAR2(600);
    n              CHAR;
    l_length       NUMBER;
    i              NUMBER;
    tmp            NUMBER;
    is_zero        BOOLEAN;
    z_count        NUMBER;
    l_money_amount NUMBER;
    l_sign         VARCHAR2(10);
  
  BEGIN
  
    l_money_amount := round(abs(p_money_amount),
                            2);
  
    IF p_money_amount < 0 THEN
      l_sign := '负';
    ELSE
      l_sign := '';
    END IF;
  
    tmp := l_money_amount * 100;
  
    c_money_amount := rtrim(ltrim(to_char(tmp,
                                          '999999999999999999')));
  
    l_length := length(c_money_amount);
  
    i := 0;
  
    WHILE i < l_length
    LOOP
    
      i := i + 1;
      n := substr(c_money_amount,
                  i,
                  1);
    
      IF n <> '0' THEN
      
        l_string := l_string || substr(n_string,
                                       to_number(n),
                                       1);
      
        IF l_length - i = 1 THEN
          l_string := l_string || '角';
        END IF;
      
        IF l_length - i = 0 THEN
          l_string := l_string || '分';
        END IF;
      
        IF l_length > 4
           AND l_length - i > 1 THEN
        
          IF MOD(l_length - i - 1,
                 4) = 0 THEN
            l_string := l_string || '仟';
          END IF;
        
          IF MOD(l_length - i,
                 4) = 0 THEN
          
            l_string := l_string || '佰';
          
          END IF;
        
          IF MOD(l_length - i + 1,
                 4) = 0 THEN
          
            l_string := l_string || '拾';
          
          END IF;
        
        ELSE
        
          IF l_length - i = 3 THEN
          
            l_string := l_string || '拾';
          
          END IF;
        
        END IF;
      
      END IF;
    
      IF n = '0' THEN
      
        IF l_length - i IN (10,
                            6,
                            2,
                            0,
                            i) THEN
        
          IF is_zero THEN
            l_string := substr(l_string,
                               1,
                               length(l_string) - 1);
            is_zero  := FALSE;
          END IF;
        
          z_count := 0;
        
        ELSE
        
          IF z_count = 0 THEN
            l_string := l_string || '零';
            is_zero  := TRUE;
          END IF;
        
          z_count := z_count + 1;
        
        END IF;
      
      ELSE
      
        z_count := 0;
        is_zero := FALSE;
      
      END IF;
    
      IF (l_length - i = 6 OR l_length - i = 14)
         AND substr(c_money_amount,
                    i - 3,
                    4) <> '0000' THEN
      
        l_string := l_string || '万';
      
      END IF;
    
      IF l_length - i = 10 THEN
      
        l_string := l_string || '亿';
      
      END IF;
    
      IF l_length - i = 2 THEN
      
        l_string := l_string || '圆';
      
      END IF;
    
    END LOOP;
  
    l_string := l_string || '整';
  
    l_string := l_sign || l_string;
  
    RETURN l_string;
  
  EXCEPTION
    WHEN OTHERS THEN
      RETURN(SQLERRM);
  END;



 FUNCTION money_format_us(p_money_amount IN NUMBER) RETURN VARCHAR2 IS
  
    TYPE lt_array_type IS VARRAY(50) OF VARCHAR2(20); --一维数组,字符串类型
    l_array lt_array_type := lt_array_type('ONE ',
                                           'TWO ',
                                           'THREE ',
                                           'FOUR ',
                                           'FIVE ',
                                           'SIX ',
                                           'SEVEN ',
                                           'EIGHT ',
                                           'NINE ',
                                           'TEN ',
                                           'ELEVEN ',
                                           'TWELVE ',
                                           'THIRTEEN ',
                                           'FOURTEEN ',
                                           'FIFTEEN ',
                                           'SIXTEEN ',
                                           'SEVENTEEN ',
                                           'EIGHTEEN ',
                                           'NINETEEN ',
                                           'TWENTY ',
                                           'THIRTY ',
                                           'FORTY ',
                                           'FIFTY ',
                                           'SIXTY ',
                                           'SEVENTY ',
                                           'EIGHTY ',
                                           'NINETY ',
                                           'HUNDRED ',
                                           'THOUSAND ',
                                           'MILLION ',
                                           'BILLION ');
  
    c_money_amount VARCHAR2(14);
    l_string       VARCHAR2(600);
    n              CHAR;
    l_pre_n        CHAR;
    l_length       NUMBER;
    i              NUMBER;
    tmp            NUMBER;
    l_decimal_flag VARCHAR2(1);
    l_money_amount NUMBER;
    l_sign         VARCHAR2(10);
  
  BEGIN
  
    l_money_amount := round(abs(p_money_amount),
                            2);
  
    IF p_money_amount < 0 THEN
      l_sign := 'NEGATIVE ';
    ELSE
      l_sign := '';
    END IF;
  
    tmp := l_money_amount * 100;
  
    c_money_amount := rtrim(ltrim(to_char(tmp,
                                          '999999999999999999')));
  
    l_length := length(c_money_amount);
  
    i := 0;
  
    WHILE i < l_length - 2
    LOOP
    
      i := i + 1;
    
      IF MOD(l_length - 2 - i,
             3) = 2 THEN
      
        n := substr(c_money_amount,
                    i,
                    1);
      
        IF n <> '0' THEN
        
          l_string := l_string || l_array(to_number(n));
          l_string := l_string || l_array(to_number(28));
        
        END IF;
      
      END IF;
    
      IF MOD(l_length - 2 - i,
             3) = 1 THEN
      
        n := substr(c_money_amount,
                    i,
                    1);
      
        IF n = '1' THEN
        
          l_pre_n := n;
        
        END IF;
      
        IF n NOT IN ('0',
                     '1') THEN
        
          l_string := l_string || l_array(to_number(n) + 18);
        
        END IF;
      
      END IF;
    
      IF MOD(l_length - 2 - i,
             3) = 0 THEN
      
        IF nvl(l_pre_n,
               'X') = '1' THEN
        
          n := substr(c_money_amount,
                      i,
                      1);
        
          l_string := l_string || l_array(to_number(l_pre_n || n));
        
          l_pre_n := NULL;
        
        ELSE
        
          n := substr(c_money_amount,
                      i,
                      1);
        
          IF n <> '0' THEN
          
            l_string := l_string || l_array(to_number(n));
          
          END IF;
        
        END IF;
      
      END IF;
    
      IF l_length - i = 5
         AND substr(c_money_amount,
                    i - 2,
                    3) <> '000' THEN
      
        l_string := l_string || l_array(to_number(29));
      
      END IF;
    
      IF l_length - i = 8
         AND substr(c_money_amount,
                    i - 2,
                    3) <> '000' THEN
      
        l_string := l_string || l_array(to_number(30));
      
      END IF;
    
      IF l_length - i = 11 THEN
      
        l_string := l_string || l_array(to_number(31));
      
      END IF;
    
    END LOOP;
  
    n := substr(c_money_amount,
                l_length - 1,
                1);
  
    IF n NOT IN ('0',
                 '1') THEN
    
      l_decimal_flag := 'Y';
      l_string       := l_string || 'AND ';
      l_string       := l_string || l_array(to_number(n) + 18);
    
    END IF;
  
    IF n = '1' THEN
    
      l_decimal_flag := 'Y';
      l_string       := l_string || 'AND ';
    
      l_pre_n := n;
    
      n := substr(c_money_amount,
                  l_length,
                  1);
    
      l_string := l_string || l_array(to_number(l_pre_n || n));
    
    ELSE
    
      n := substr(c_money_amount,
                  l_length,
                  1);
    
      IF n <> '0' THEN
      
        IF nvl(l_decimal_flag,
               'N') = 'N' THEN
        
          l_string := l_string || 'AND ';
        
        END IF;
      
        l_decimal_flag := 'Y';
      
        l_string := l_string || l_array(to_number(n));
      
      END IF;
    
    END IF;
  
    IF l_decimal_flag = 'Y' THEN
    
      l_string := l_string || 'CENTS ';
    
    ELSE
    
      l_string := l_string;
    
    END IF;
  
    l_string := l_sign || l_string || 'ONLY ';
  
    RETURN l_string;
  
  EXCEPTION
    WHEN OTHERS THEN
      RETURN(SQLERRM);
  END;





数字金额大小写转换

标签:

原文地址:http://www.cnblogs.com/yxwkf/p/4580618.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!