【问题标题】:How to always show two decimal places on a number in PL/SQL如何始终在 PL/SQL 中的数字上显示两位小数
【发布时间】: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


    【解决方案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;
    

    【讨论】:

    • 更新了原始问题,提供了更多信息。我实际上需要从数据库中获取数字并将其作为字符串保存到一个变量中,该字符串始终包含两个小数,因为这个过程返回的是多个字符串连接成一个字符串。没有最终插入到数据库中,它是数据的所有格式并返回它。
    • @Jchieppa Please Check this fiddle URL 。您的问题陈述无效。
    • @Jchieppa 我猜唯一的可能性是,你可能执行了没有 TO_CHAR() 的代码版本,你在这里注释掉了。
    猜你喜欢
    • 2013-06-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-06-01
    • 1970-01-01
    • 2014-05-03
    • 1970-01-01
    • 2022-12-07
    相关资源
    最近更新 更多