【发布时间】:2015-10-20 20:09:25
【问题描述】:
伙计们,
我有一个现有的存储过程,我正在尝试更新它以始终显示两位小数,即使它是整数或单个小数。
此存储过程构建了一条消息,该消息必须将 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 格式化可以解决这个问题,ala
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 列中的值是一个不带小数的整数,或者像 250.5 v_credit_amt 这样的数字在查询我要插入以进行调试的 SQL_TXT 表时在所有情况下都显示相同。
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;
但是,我无法让最终字符串在所有用例中都正确包含两位小数。
例如,如果 db 中的值为 125,我会得到一个最终字符串
144450034629999008009Bob Smith05900100{89006125}1060073307542164001017400340917500
34091830010
应该是这样的
144450034629999008009Bob Smith05900100{89007125.00}1060073307542164001017400340917500
34091830010
抱歉上面的格式,我看不到如何在没有代码块的情况下加粗一个部分,所以我突出显示了相关部分而不是{}
如果我需要始终将一个数字显示为两位小数,即使给出了整数或一位小数,我错过了什么?
【问题讨论】:
标签: sql oracle plsql number-formatting