-- Converts a given decimal number to binary.
-- Ignores decimals - only converts integer portion of a number,
-- but it retains the sign.
CREATE OR REPLACE FUNCTION f_to_binary(in_num IN NUMBER) RETURN NUMBER IS
ln_max NUMBER := FLOOR(LN(ABS(in_num)) / LN(2));
ln_num NUMBER := ABS(in_num);
lv_bin VARCHAR2(4000) := '0';
ln_Sign NUMBER;
BEGIN
IF in_num >= 0 THEN
ln_Sign := 1;
ELSE
ln_Sign := -1;
END IF;
FOR i IN REVERSE 0 .. ln_max LOOP
IF ln_num >= POWER(2, i) THEN
lv_bin := lv_bin || '1';
ln_num := ln_num - POWER(2, i);
ELSE
lv_bin := lv_bin || '0';
END IF;
END LOOP;
RETURN TO_NUMBER(lv_bin) * ln_Sign;
END f_to
/
Some More Important PL/SQL Scripts:
-- Ignores decimals - only converts integer portion of a number,
-- but it retains the sign.
CREATE OR REPLACE FUNCTION f_to_binary(in_num IN NUMBER) RETURN NUMBER IS
ln_max NUMBER := FLOOR(LN(ABS(in_num)) / LN(2));
ln_num NUMBER := ABS(in_num);
lv_bin VARCHAR2(4000) := '0';
ln_Sign NUMBER;
BEGIN
IF in_num >= 0 THEN
ln_Sign := 1;
ELSE
ln_Sign := -1;
END IF;
FOR i IN REVERSE 0 .. ln_max LOOP
IF ln_num >= POWER(2, i) THEN
lv_bin := lv_bin || '1';
ln_num := ln_num - POWER(2, i);
ELSE
lv_bin := lv_bin || '0';
END IF;
END LOOP;
RETURN TO_NUMBER(lv_bin) * ln_Sign;
END f_to
/
Some More Important PL/SQL Scripts:
- PL/SQL Function To Compute The Factorial Of A Number
- PL/SQL Function To Convert A Binary Number To A Decimal Number
- PL/SQL Function To Convert Ruppies(Numbers) In Words
- PL/SQL Function To Generate The Fibonacci Series
- PL/SQL Procedure For Counting All Tables And Respective Rows From Database
- PL/SQL Procedure To Display Monthly Calender
- PL/SQL Procedure To Reverse A String
- PL/SQL Script To Calculate Weekdays Between Two Given Dates
COMMENTS