Please see my other blog for Oracle EBusiness Suite Posts - EBMentors

Search This Blog

Note: All the posts are based on practical approach avoiding lengthy theory. All have been tested on some development servers. Please don’t test any post on production servers until you are sure.

Tuesday, October 04, 2011

Spell the numbers - Examples

Using JSP format

SELECT TO_CHAR(TO_DATE(123.50,'J'),'JSP') to_words FROM   dual;

SELECT    TO_CHAR (TO_DATE (TRUNC (&num), 'J'), 'JSP')
       || ' Point '
       || TO_CHAR (TO_DATE (TO_NUMBER (SUBSTR (&num, INSTR (&num, '.') + 1)),'J'),'JSP')
  FROM DUAL;
Another Way
with sample_data as ( select level num
                         from dual
                         connect by level <=8
                        )                 
    select num
    ,      to_char( to_timestamp( lpad( num, 9, '0'), 'FF9' ), 'Ffsp' ) words1
,      to_char( to_timestamp( lpad( num, 9, '0'), 'FF9' ), 'Ffspth' )  words2
   from   sample_data

Function Example:
without Decimal Places
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 :=
               TO_CHAR (TO_DATE (SUBSTR (l_num, LENGTH (l_num) - 2, 3), 'J'),
                        'Jsp'
                       )
            || l_str (i)
            || l_return;
      END IF;

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

   RETURN l_return;
END;
/

With Decimal Places
create or replace
   function spell_number2( p_number in number )
    return varchar2
    -- modified to include decimal places
    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 := to_char(
                               to_date(
                                substr(l_num, length(l_num)-2, 3),
                                  'J' ),
                           'Jsp' ) || l_str(i) || l_return;
          end if;
           l_num := substr( l_num, 1, length(l_num)-3 );
       end loop;
  
       -- beginning of section added to include decimal places:
       if to_char( p_number ) like '%.%'
       then
           l_num := substr( p_number, instr( p_number, '.' )+1 );
           if l_num > 0
           then
               l_return := l_return || ' point';
               for i in 1 .. length (l_num)
               loop
                   exit when l_num is null;
                   if substr( l_num, 1, 1 ) = '0'
                   then
                       l_return := l_return || ' zero';
                   else
                      l_return := l_return
                       || ' '
                       || to_char(
                              to_date(
                              substr( l_num, 1, 1),
                                'j' ),
                          'jsp' );
                   end if;
                   l_num := substr( l_num, 2 );
               end loop;
           end if;
       end if;
       -- end of section added to include decimal places
  
       return l_return;
   end spell_number2;
   /

No comments: