繁体   English   中英

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

[英]How to always show two decimal places on a number in PL/SQL

民间,

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

此存储过程生成了一条消息,该消息必须将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;

我认为在选择过程中通过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;

从上面的注释行中可以看到,一旦定义了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;

但是,如果存储在sit_credit_limit列中的值是不带小数的整数,或者在查询要插入以进行调试的SQL_TXT表时,在所有情况下,v_credit_amt之类的数字v_credit_amt显示为250.5。

SQL> select * from SQL_TXT;

COL1
--------------------------------------------------------------------------------
175
250.5

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

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;

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

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

144450034629999008009Bob Smith05900100{89006125}1060073307542164001017400340917500 34091830010

但是它应该是

144450034629999008009Bob Smith05900100{89007125.00}1060073307542164001017400340917500 34091830010

抱歉,上面的格式,我看不到如何在没有代码块的情况下加粗部分,所以我突出显示了相对部分{

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

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

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

insert into SQL_TXT values (TO_CHAR(v_credit_amt,'FM99999.00'));
commit;

如果INSERT-ed列再次为NUMBER

你还是想和

SELECT TO_CHAR(COL1,'FM99999.00') FROM SQL_TEXT;

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM