Search code examples
sqloracleplsqloracle-sqldeveloperplsqldeveloper

Incorrect number format (.0115 instead of 0.115) after the cursor execution


I try to execute this code, but get incorrect type of numbers in the output.I've tried to change (precision, scale) for source table change NLS_NUMERIC_CHARACTERS but no results.

SET SERVEROUTPUT ON;
DECLARE
--Declare variables
  v_pr number ;
  v_fin number ;
--Declare cursor 
CURSOR cur_price IS
  SELECT price FROM prices;
BEGIN
OPEN cur_price; 
LOOP      --Simple Loop Start
  FETCH cur_price INTO v_pr; 
  v_fin := v_pr * 1.15; --Calculation
  DBMS_OUTPUT.PUT_LINE (to_char(v_pr) ||' --> '|| to_char(v_fin));  --Show the result
  EXIT WHEN cur_price%NOTFOUND; 
END LOOP; --Simple Loop End
CLOSE cur_price;
END;
-----------------
.01 --> .0115
.02 --> .023
.03 --> .0345
.03 --> .0345

Table parameters:

select * from prices
----------------
0.01
0.02
0.03


desc prices
Name  Null? Type        
----- ----- ----------- 
PRICE       NUMBER(6,3)

NLS parameters:

select value
from nls_session_parameters
where parameter = 'NLS_NUMERIC_CHARACTERS';

VALUE
-------------------------------
.,

Solution

  • Oracle Database stores numeric data in variable-length format. Each value is stored in scientific notation, with 1 byte used to store the exponent. The database uses up to 20 bytes to store the mantissa, which is the part of a floating-point number that contains its significant digits. Oracle Database does not store leading and trailing zeros.

    SQL> create table prices ( price number(6,3) );
    
    Table created.
    
    SQL> insert into prices values ( 0.293 );
    
    1 row created.
    
    SQL> commit;
    
    Commit complete.
    
    SQL> select * from prices;
    
         PRICE
    ----------
          .293
    
    SQL> alter session set nls_numeric_characters=',.' ;
    
    Session altered.
    
    SQL> select * from prices;
    
         PRICE
    ----------
          ,293
    
    SQL> SET SERVEROUTPUT ON;
    DECLARE
    --Declare variables
      v_pr number ;
      v_fin number ;
    --Declare cursor
    CURSOR cur_price IS
      SELECT price FROM prices;
    BEGIN
    OPEN cur_price;
    LOOP      --Simple Loop Start
      FETCH cur_price INTO v_pr;
      v_fin := v_pr * 1.15; --Calculation
      DBMS_OUTPUT.PUT_LINE (to_char(v_pr) ||' --> '|| to_char(v_fin));  --Show the result
      EXIT WHEN cur_price%NOTFOUND;
    END LOOP; --Simple Loop End
    CLOSE cur_price;
    END;
    /
    
    ,293 --> ,33695
    ,293 --> ,33695