We must be bored.....
Write a function to write out a number in words using the least code.
Rules:
1, Pick a language.
2, This is not an obfuscated code contest :P
3, um....?
So far I am ahead at 48 lines writing in Oracle PL/SQL:
Output at bottom of post.
output:
If I have time I'll translate it into c at home.
chuck
Write a function to write out a number in words using the least code.
Rules:
1, Pick a language.
2, This is not an obfuscated code contest :P
3, um....?
So far I am ahead at 48 lines writing in Oracle PL/SQL:
Output at bottom of post.
Code:
CREATE OR REPLACE function TAX.write_num(v_arg in number) return char as v_in varchar2(256) := ltrim(to_char(v_arg, '99999999999999999')); v_out varchar2(256) := ''; v_curr varchar2(256); v_next varchar2(256); v_big varchar2(256) := ''; v_tail number := length(v_in); begin if v_tail > 0 then if length(v_in) > 12 then v_big := 'TRILLION '; elsif length(v_in) > 9 then v_big := 'BILLON '; elsif length(v_in) > 6 then v_big := 'MILLION '; elsif length(v_in) > 3 then v_big := 'THOUSAND '; end if; while length(substr(v_in,1, v_tail)) > 3 loop v_tail := v_tail - 3; end loop; v_curr := ltrim(to_char(to_number(substr(v_in, 1, v_tail)), '900')); v_next := tax.write_num(substr(v_in, v_tail + 1)); if length(v_curr) = 3 then v_out := tax.write_num(substr(v_curr, 1, 1)) || 'HUNDRED ' || tax.write_num(substr(v_curr, 2, 2)); elsif v_curr = '00' then v_out := 'ZERO'; elsif v_curr = '01' then v_out := 'ONE '; elsif v_curr = '02' then v_out := 'TWO '; elsif v_curr = '03' then v_out := 'THREE '; elsif v_curr = '04' then v_out := 'FOUR '; elsif v_curr = '05' then v_out := 'FIVE '; elsif v_curr = '06' then v_out := 'SIX '; elsif v_curr = '07' then v_out := 'SEVEN '; elsif v_curr = '08' then v_out := 'EIGHT '; elsif v_curr = '09' then v_out := 'NINE '; elsif v_curr = '10' then v_out := 'TEN '; elsif v_curr = '11' then v_out := 'ELEVEN '; elsif v_curr = '12' then v_out := 'TWELVE '; elsif substr(v_curr, 1, 1) = '1' then v_out := rtrim(replace(replace(tax.write_num(substr(v_curr, 2, 1)), 'VE', 'F'),'REE','IR')) || 'TEEN '; elsif substr(v_curr, 1, 1) = '2' then v_out := 'TWENTY ' || tax.write_num(substr(v_curr, 2, 2)); elsif substr(v_curr, 1, 1) = '3' then v_out := 'THIRTY ' || tax.write_num(substr(v_curr, 2, 2)); elsif substr(v_curr, 1, 1) = '4' then v_out := 'FOURTY ' || tax.write_num(substr(v_curr, 2, 2)); elsif substr(v_curr, 1, 1) = '5' then v_out := 'FIFTY ' || tax.write_num(substr(v_curr, 2, 2)); elsif substr(v_curr, 1, 1) = '6' then v_out := 'SIXTY ' || tax.write_num(substr(v_curr, 2, 2)); elsif substr(v_curr, 1, 1) = '7' then v_out := 'SEVENTY ' || tax.write_num(substr(v_curr, 2, 2)); elsif substr(v_curr, 1, 1) = '8' then v_out := 'EIGHTY ' || tax.write_num(substr(v_curr, 2, 2)); elsif substr(v_curr, 1, 1) = '9' then v_out := 'NINETY ' || tax.write_num(substr(v_curr, 2, 2)); end if; v_out := v_out || v_big || v_next; if substr(v_out, 1, 4) <> 'ZERO' then v_out := replace(v_out, 'ZERO', ''); end if; return v_out; end if; return ''; end;
Code:
SQL> select write_num(98765432115) 2 from dual; WRITE_NUM(98765432115) -------------------------------------------------------------------------------- NINETY EIGHT BILLON SEVEN HUNDRED SIXTY FIVE MILLION FOUR HUNDRED THIRTY TWO THOUSAND ONE HUNDRED FIFTEEN SQL> select write_num(13000915) 2 from dual; WRITE_NUM(13000915) ------------------------------------------ THIRTEEN MILLION NINE HUNDRED FIFTEEN SQL>
chuck
Comment