数字金额大写和小写转换

有非常多时候打印凭据的时候须要实现金额大写。比如

有多种写法来实现

比如一下几种

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:@"á?", @"ò?", @"·?", @"èt", @"?á", @"?é", @"??", @"?a", @"°?", @"?á", nil];
    NSArray *arr_numbers = [[NSArray alloc] initWithObjects:@"Zero", @"One", @"Two", @"Three", @"Four", @"Five", @"Six", @"Seven", @"Eight", @"Nine", nil];

    //NSArray *arr_unit = [[NSArray alloc] initWithObjects:@"·?", @"??", @"?2", @"ê°", @"°?", @"?a", @"íò", @"ê°", @"°?", @"?a", @"òú", @"ê°", @"°?", @"?a", @"?×", @"ê°", @"°?", @"?a",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 [email protected]"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;
时间: 2024-10-08 23:15:37

数字金额大写和小写转换的相关文章

C++中字母大写和小写转换实现的优化

C++中字母大写和小写转换实现的优化 write by 九天雁翎(JTianLing) -- blog.csdn.net/vagrxie 讨论新闻组及文件 在本文中所有以转换为小写为例. 从推荐复用代码的角度来看,用库函数是不错的办法: 方案一: char gc1[53] = "abcdefghigklmnopqrstuvwxyzABCDEFGHIGKLMNOPQRSTUVWXYZ"; void wayOne() { strlwr(gc1); } 长处是使用方便,别人看着也easy理解

数字按照不同格式转换成字符串

如果自己写函数,不使用itoa怎么判断呢? 我们用通常的办法,对数字进行每位的除商,得到后与字符'0'相加. flag = 0; for(i=0;i<6;i++){ tmp = int(num/pow(10,5-i)); if(tmp != 0){ *str = 1; flag = 1; } if(*str != 0 || flag){ *str++ = tmp+'0'; num = num%int(pow(10,5-i)); } } 要注意的就是,我们设置标志位flag,为1之前的所有0都不输

Swift数字类型之间的转换

Swift数字类型之间的转换Swift是一种安全的语言,对于类型的检查非常严格,不同类型之间不能随便转换.一.整型之间的转换在C和Objective-C等其他语言中,整型之间有两种转换方法:从小范围数到大范围数转换是自动的:从大范围数到小范围数需要强制类型转换,有可能造成数据精度的丢失.而在Swift中这两种方法是行不通的,我们需要通过一些函数进行显式地转换,代码如下: let historyScore:UInt8 = 90 let englishScore:UInt16 = 130 let t

shell中大小写转换

有两种方式: 1.用tr 例如:UPPERCASE=$(echo $VARIABLE | tr '[a-z]' '[A-Z]')   (把VARIABLE的小写转换成大写) LOWERCASE=$(echo $VARIABLE | tr '[A-Z]' '[a-z]')   (把VARIABLE的大写转换成小写) 2.用typeset typeset -u VARIABLE  (把VARIABLE的小写转换成大写) typeset -l  VARIABLE  (把VARIABLE的大写转换成小写

mysql - varchar类型与数字的比较和转换

mysql - varchar类型与数字的比较和转换 convert(对象, 目标类型)是mysql中常用的类型转换对象函数.eg: select convert('1.123', decimal(10.4)),结果就是1.1230.对象可以用列名替代. 前两天发现,一个小伙伴之前设计表时把某个表数据类型设计成了varchar,实际用于存储Decimal.我需要用其数据进行过滤筛选 如果文章内容有问题,欢迎评论或与我进行讨论(请注明原因): mail: [email protected] 微信:

(数字类型,进制转换,字符串,列表)介绍和操作

数字类型 整型 用途:记录年龄.等级.号码等 定义方式如 age = 18 本质 # age = int(18) 整型只能存一个值而且是不可变类型 int()方法能将字符串转为整型 需要注意的是使用int只能转纯数字的字符串,有小数点也不行 浮点型 用途:记录身高.体重.薪资等 定义方式如 height= 185.1 本质 # height= float(185.1) 浮点型只能存一个值而且是不可变类型 float()方法能将字符串和整型转为浮点型 需要注意的是使用float方法只能转纯数字的字

C++中数字和字符串的转换

1.字符串数字之间的转换 (1)string --> char *   string str("OK");   char * p = str.c_str(); (2)char * -->string   char *p = "OK";   string str(p); (3)char * -->CString    char *p ="OK";   CString m_Str(p);   //或者   CString m_Str

money 小写转换大写

示例代码如下: function convertCurrency(money) { //汉字的数字 var cnNums = new Array('零', '壹', '贰', '叁', '肆', '伍', '陆', '柒', '捌', '玖'); //基本单位 var cnIntRadice = new Array('', '拾', '佰', '仟'); //对应整数部分扩展单位 var cnIntUnits = new Array('', '万', '亿', '兆'); //对应小数部分单位

javascript中字符串向数字类型的自动转换

js中类型的转换依环境而定,当字符串向数字类型(浮点)转换时,有几种情况: 1.加号连接符引导为字符拼接: console.log("2"+1); 21 console.log(1+"2"+1); 121 2.其余情况引导为数字计算: console.log(0-"2"+1); -1 console.log("2"*2); 4 console.log(1*"2"+1); 3 3.当字符串带非数字时返回NaN