将数字转换为单词

时间:2016-10-12 15:44:13

标签: sql oracle plsql

我有一个将单词转换为数字的功能,但当数字为例如' 4500'给你的输出是“四千五百”'

我希望在结果

的结尾删除AND

以下是功能:

CREATE OR REPLACE FUNCTION spell_number (p_number IN NUMBER)
   RETURN VARCHAR2
AS
   TYPE myArray IS TABLE OF VARCHAR2 (255);

   l_str      myArray
                 := myArray ('',
                             ' thousand ',
                             ' million ',
                             ' billion ',
                             ' trillion ',
                             ' quadrillion ',
                             ' quintillion ',
                             ' sextillion ',
                             ' septillion ',
                             ' octillion ',
                             ' nonillion ',
                             ' decillion ',
                             ' undecillion ',
                             ' duodecillion ');

   l_num      VARCHAR2 (50) DEFAULT TRUNC (p_number);
   l_return   VARCHAR2 (4000);
BEGIN
   FOR i IN 1 .. l_str.COUNT
   LOOP
      EXIT WHEN l_num IS NULL;

      IF (SUBSTR (l_num, LENGTH (l_num) - 2, 3) <> 0)
      THEN
         l_return :=
               REPLACE (
                     TO_CHAR (
                        TO_DATE (SUBSTR (l_num, LENGTH (l_num) - 2, 3), 'J'),
                        'Jsp')
                  || l_str (i),
                  'Hundred',
                  'Hundred AND')
            || l_return;
      END IF;

      l_num := SUBSTR (l_num, 1, LENGTH (l_num) - 3);
   END LOOP;

   RETURN l_return;
END;

2 个答案:

答案 0 :(得分:1)

如果您只想删除最终的'AND',则可以通过以下方式编辑代码:

SQL> CREATE OR REPLACE FUNCTION spell_number (p_number IN NUMBER)
  2     RETURN VARCHAR2
  3  AS
  4     TYPE myArray IS TABLE OF VARCHAR2 (255);
  5
  6     l_str      myArray
  7                   := myArray ('',
  8                               ' thousand ',
  9                               ' million ',
 10                               ' billion ',
 11                               ' trillion ',
 12                               ' quadrillion ',
 13                               ' quintillion ',
 14                               ' sextillion ',
 15                               ' septillion ',
 16                               ' octillion ',
 17                               ' nonillion ',
 18                               ' decillion ',
 19                               ' undecillion ',
 20                               ' duodecillion ');
 21
 22     l_num      VARCHAR2 (50) DEFAULT TRUNC (p_number);
 23     l_return   VARCHAR2 (4000);
 24  BEGIN
 25     FOR i IN 1 .. l_str.COUNT
 26     LOOP
 27        EXIT WHEN l_num IS NULL;
 28
 29        IF (SUBSTR (l_num, LENGTH (l_num) - 2, 3) <> 0)
 30        THEN
 31           l_return :=
 32                 REPLACE (
 33                       TO_CHAR (
 34                          TO_DATE (SUBSTR (l_num, LENGTH (l_num) - 2, 3), 'J'),
 35                          'Jsp')
 36                    || l_str (i),
 37                    'Hundred',
 38                    'Hundred AND')
 39              || l_return;
 40        END IF;
 41
 42        l_num := SUBSTR (l_num, 1, LENGTH (l_num) - 3);
 43     END LOOP;
 44
 45     RETURN rtrim(l_return, 'AND');
 46  END;
 47  /

Function created.

SQL> select spell_number('4500') from dual;

SPELL_NUMBER('4500')
--------------------------------------------------------------------------------
Four thousand Five Hundred

SQL>

答案 1 :(得分:0)

根据我的理解,你希望在hundreeds和数十或数字之间看到AND 我是说&#39; 556003000&#39;应该是“五百五十六万三千三百万”。 和&#39; 500003000&#39;应该是“五亿三千”没有AND

您可以添加支票

cur_num := SUBSTR (l_num, LENGTH (l_num) - 2, 3);
IF MOD(cur_num, 100) != 0 THEN
  ... REPLACE ( TO_CHAR (
                       TO_DATE (SUBSTR (l_num, LENGTH (l_num) - 2, 3), 'J'),
                       'Jsp')
                 || l_str (i),
                 'Hundred',
                 'Hundred AND');
  ... 
ELSE 
  ...
  TO_CHAR (
                       TO_DATE (SUBSTR (l_num, LENGTH (l_num) - 2, 3), 'J'),
                       'Jsp')
                 || l_str (i);
  ... 
END IF; 

更改的功能看起来像

CREATE OR REPLACE FUNCTION spell_number (p_number IN NUMBER)
   RETURN VARCHAR2
AS
   TYPE myArray IS TABLE OF VARCHAR2 (255);

   l_str      myArray
                 := myArray ('',
                             ' thousand ',
                             ' million ',
                             ' billion ',
                             ' trillion ',
                             ' quadrillion ',
                             ' quintillion ',
                             ' sextillion ',
                             ' septillion ',
                             ' octillion ',
                             ' nonillion ',
                             ' decillion ',
                             ' undecillion ',
                             ' duodecillion ');

   l_num      VARCHAR2 (50) DEFAULT TRUNC (p_number);
   l_return   VARCHAR2 (4000);
   l_cur_num  VARCHAR2(3); 
   l_cur_text  VARCHAR2(4000); 
BEGIN
   FOR i IN 1 .. l_str.COUNT
   LOOP
      EXIT WHEN l_num IS NULL;
      l_cur_num := SUBSTR (l_num, LENGTH (l_num) - 2, 3);

      IF (l_cur_num <> 0)
      THEN
    l_cur_text := TO_CHAR ( TO_DATE (l_cur_num, 'J'), 'Jsp')|| l_str (i); 
        IF MOD(l_cur_num,100) != 0 THEN 
          l_cur_text := REPLACE (l_cur_text, 'Hundred', 'Hundred AND');
        END IF;  
        l_return := l_cur_text || l_return;
      END IF;

      l_num := SUBSTR (l_num, 1, LENGTH (l_num) - 3);
   END LOOP;

   RETURN l_return;
END;