科技行者

行者学院 转型私董会 科技行者专题报道 网红大战科技行者

知识库

知识库 安全导航

至顶网软件频道Oracle中将数字转化成字符串

Oracle中将数字转化成字符串

  • 扫一扫
    分享文章到微信

  • 扫一扫
    关注官方公众号
    至顶头条

  CREATE OR REPLACE FUNCTION to_words (     m_amount_in IN NUMBER,     m_first_para IN VARCHAR2 := 'Dollars',     m_second_pa

作者:中国IT实验室 来源:中国IT实验室 2007年10月6日

关键字: 开发 数据库 ORACLE

  • 评论
  • 分享微博
  • 分享邮件

  CREATE OR REPLACE FUNCTION to_words (
    m_amount_in IN NUMBER,
    m_first_para IN VARCHAR2 := 'Dollars',
    m_second_para IN VARCHAR2 := 'Cents'
    )
    RETURN VARCHAR2
  AS
    m_amount   NUMBER := m_amount_in;
    word_string VARCHAR2(1000);
    lower_number NUMBER;
    lower_str  VARCHAR2(50);
    loop_var   NUMBER := 1;
    /* Function declarartion to convert into string */
    FUNCTION to_string (m_val_in NUMBER)
     RETURN CHAR
    IS
    BEGIN
     IF m_val_in > 0
     THEN
       RETURN (INITCAP (TO_CHAR (TO_DATE (m_val_in, 'SSSSS'), 'SSSSSSP')));
     ELSE
       RETURN ('');
     END IF;
    END;
    /*     End of Function   */
  BEGIN
  
  /*-------------- place the parameters at required places --------------*/
    lower_number := (m_amount MOD 1) * 100;
      /* Check for lower denominations not exceeding two digits */
    IF length(lower_number) < 3
    THEN
    IF m_amount >= 1 and lower_number > 0
     THEN  lower_str :=
       ' ' ||
        m_first_para ||
       ' & ' ||
        LOWER (to_string (lower_number)) ||
       ' ' ||
        m_second_para;
     ELSIF m_amount < 1 and lower_number > 0
     THEN  lower_str :=
       LOWER (to_string (lower_number)) ||
        ' ' ||
       m_second_para;
     ELSIF m_amount >= 1 and lower_number = 0
     THEN  lower_str := ' ' || m_first_para;
     END IF;
    ELSE
        word_string := 'Invalid lower denomination !!' ;
     RETURN (word_string);
    END IF;
  /*---------------- End of placing paramters -------------------------*/
  
    WHILE loop_var = 1
    LOOP
     IF   m_amount > 0
       AND m_amount <= 1000
     THEN
       word_string :=
        word_string ||
         (INITCAP (to_string (FLOOR (m_amount))) || lower_str);
       loop_var := 0;
     ELSIF   m_amount > 1000
        AND m_amount < 1000000
     THEN
       word_string :=
        word_string ||
         (INITCAP (to_string (FLOOR (m_amount / 1000))) ||
        ' Thousand ' ||
        to_string (FLOOR (m_amount MOD 1000)) ||
        lower_str
          );
       loop_var := 0;
     ELSIF   m_amount >= 1000000
        AND m_amount < 1000000000
     THEN
       word_string :=
        word_string ||
        INITCAP (to_string (FLOOR (m_amount / 1000000))) || ' Million ';
       m_amount := FLOOR (m_amount MOD 1000000);
       IF m_amount = 0
       THEN
        loop_var := 0;
        word_string := word_string || lower_str;
       END IF;
     ELSIF   m_amount >= 1000000000
        AND m_amount <= 999999999999.99
     THEN
       word_string :=
        INITCAP (
          to_string (FLOOR (m_amount / 1000000000))) || ' Billion ';
       m_amount := FLOOR (m_amount MOD 1000000000);
       IF m_amount = 0
       THEN
        loop_var := 0;
        word_string := word_string || lower_str;
       END IF;
     ELSIF m_amount = 0
     THEN
       word_string := ('Zero' || lower_str);
       loop_var := 0;
     ELSE
       word_string := 'This is beyond my scope !!';
       loop_var := 0;
     END IF;
    END LOOP;
    RETURN (word_string);
  END to_words;
  /
  
  
  

查看本文来源

    • 评论
    • 分享微博
    • 分享邮件
    邮件订阅

    如果您非常迫切的想了解IT领域最新产品与技术信息,那么订阅至顶网技术邮件将是您的最佳途径之一。

    重磅专题
    往期文章
    最新文章