随笔 - 13, 文章 - 0, 评论 - 1, 引用 - 0
数据加载中……

金额转换

CREATE OR REPLACE FUNCTION money_to_chinese (money IN VARCHAR2)
   RETURN VARCHAR2
IS
   c_money    VARCHAR2 (12);
   m_string   VARCHAR2 (60) := '分角圆拾佰仟万拾佰仟亿';
   n_string   VARCHAR2 (40) := '壹贰叁肆伍陆柒捌玖';
   b_string   VARCHAR2 (80);
   n          CHAR;
   len        NUMBER (3);
   i          NUMBER (3);
   tmp        NUMBER (12);
   is_zero    BOOLEAN;
   z_count    NUMBER (3);
   l_money    NUMBER;
   l_sign     VARCHAR2 (10);
BEGIN
   l_money := ABS (money);

   IF money < 0
   THEN
      l_sign := '负';
   ELSE
      l_sign := '';
   END IF;

   tmp := ROUND (l_money, 2) * 100;
   c_money := RTRIM (LTRIM (TO_CHAR (tmp, '999999999999')));
   len := LENGTH (c_money);
   is_zero := TRUE;
   z_count := 0;
   i := 0;

   WHILE i < len
   LOOP
      i := i + 1;
      n := SUBSTR (c_money,
                   i,
                   1
                  );

      IF n = '0'
      THEN
         IF len - i = 6 OR len - i = 2 OR len = i
         THEN
            IF is_zero
            THEN
               b_string := SUBSTR (b_string,
                                   1,
                                   LENGTH (b_string) - 1
                                  );
               is_zero := FALSE;
            END IF;

            IF len - i = 6
            THEN
               b_string := b_string || '万';
            END IF;

            IF len - i = 2
            THEN
               b_string := b_string || '元';
            END IF;

            IF len = i
            THEN
               b_string := b_string || '整';
            END IF;

            z_count := 0;
         ELSE
            IF z_count = 0
            THEN
               b_string := b_string || '零';
               is_zero := TRUE;
            END IF;

            z_count := z_count + 1;
         END IF;
      ELSE
         b_string :=
               b_string
            || SUBSTR (n_string,
                       TO_NUMBER (n),
                       1
                      )
            || SUBSTR (m_string,
                       len - i + 1,
                       1
                      );
         z_count := 0;
         is_zero := FALSE;
      END IF;
   END LOOP;

   b_string := l_sign || b_string;
   RETURN b_string;
EXCEPTION
   WHEN OTHERS
   THEN
      RETURN (SQLERRM);
END;

posted on 2013-02-04 08:39 李鑫 阅读(169) 评论(0)  编辑 收藏 引用 所属分类: oracle 函数


只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   博问   Chat2DB   管理