如何在PL / SQL中始终显示数字的小数点后两位

Jch*_*ppa 3 sql oracle plsql number-formatting

民间,

我有一个现有的存储过程,我试图将其更新为始终显示两位小数,即使它是整数还是一个小数。

此存储过程建立了一条消息,必须显示 v_credit_amt为两个十进制数,但是分配给v_credit_amt它的值可以是整数或单个十进制或两个十进制值,即175应该显示为175.00,250.5应该显示为250.50,395.95应该显示为395.95。

这是相关的库存pl / sql

v_credit_amt        number(22,2);
select NVL(sit_credit_limit,0)
  into v_credit_amt
  from sites
 where sit_pkey = p_site_id;
Run Code Online (Sandbox Code Playgroud)

我认为在选择过程中通过to_char格式化会解决这个问题,

select NVL(to_char(sit_credit_limit, 'fm99999.00'),0)
  into v_credit_amt
  from sites
 where sit_pkey = p_site_id;

--v_credit_amt := to_char(v_credit_amt, 'fm99999.00');
insert into SQL_TXT values (v_credit_amt);
commit;
Run Code Online (Sandbox Code Playgroud)

从上面的注释行中可以看到,定义了v_credit_amt变量后,我也尝试过

我也尝试过to_number函数

select NVL(to_number(sit_credit_limit, '99999.00'),0)
  into v_credit_amt
  from sites
 where sit_pkey = p_site_id;
Run Code Online (Sandbox Code Playgroud)

但是,如果存储在sit_credit_limit列中的值是不带小数的整数,或者v_credit_amt在查询要插入的SQL_TXT表时,在所有情况下类似250.5的数字在所有情况下都表示相同。

SQL> select * from SQL_TXT;

COL1
--------------------------------------------------------------------------------
175
250.5
Run Code Online (Sandbox Code Playgroud)

这个特定事件只是将多个消息部分串联成一个单个的长消息字符串,即返回

if p_message_id = '14' then
   v_message := 'Comtrol Check In Message Posted';
   v_string  := v_string || '008' || lpad(length(v_fname || ' ' || v_lname), 3, '0') || v_fname || ' ' || v_lname;
   v_string  := v_string || '059' || lpad(1, 3, '0') || '0';
   --v_string  := v_string || '089' || lpad(1, 3, v_credit_amt) || to_char(v_credit_amt);
   v_string  := v_string || '089' || lpad(length(to_char(v_credit_amt, 'fm9999999.00')), 3, '0') || to_char(v_credit_amt, 'fm9999999.00');
   v_string  := v_string || '106' || lpad(length(nvl(v_acct_number,'')), 3, '0') || v_acct_number;
   --v_string  := v_string || '106' || v_acct_number;
   v_string  := v_string || '164' || lpad(1, 3, '0') || '0';
   v_string  := v_string || '174' || lpad(length(v_rm_phone_num), 3, '0') || v_rm_phone_num;
   v_string  := v_string || '175' || lpad(length(v_rm_id), 3, '0') || v_rm_id;
   v_string  := v_string || '183' || lpad(1, 3, '0') || '0';
endif;
Run Code Online (Sandbox Code Playgroud)

但是,在所有用例中,我无法使最终字符串正确包含两个小数。

例如,如果数据库中的值是125,则得到最终字符串

144450034629999008009Bob Smith05900100{89006125}1060073307542164001017400340917500 34091830010

但是它应该是

144450034629999008009Bob Smith05900100{89007125.00}1060073307542164001017400340917500 34091830010

抱歉,上面的格式,我看不到没有代码块的部分如何加粗,所以我反而突出了相关部分{}

如果即使给出整数或1个十进制值,也总是需要显示两位小数,我会丢失什么?

Mah*_*kar 7

您实际上必须使用格式化插入数据。(假设目标列是VARCHAR)无论您获取并放入NUMBER变量的格式是NUMBER。

毕竟,数字没有要保存的格式。仅用于显示,格式出现在图片中。

insert into SQL_TXT values (TO_CHAR(v_credit_amt,'FM99999.00'));
commit;
Run Code Online (Sandbox Code Playgroud)

如果再次是INSERT-ed列NUMBER。

你还是想和

SELECT TO_CHAR(COL1,'FM99999.00') FROM SQL_TEXT;
Run Code Online (Sandbox Code Playgroud)