繁体   English   中英

如何使用千位分隔符将数字格式化为varchar? 像oracle to_char()

[英]How to format number to varchar with thousand separator? Like oracle to_char()

谁知道如何在 Firebird 3 格式中将数字转换为带有千位分隔符的 varchar? 像oracle to_char() select to_char(1234325234234.55,'999G999G999G999G999D00') nn from dual -- 1 234 325 234 234,55

Firebird 中没有这样的选项。 如果您需要类似的东西,您需要将它自己构建为 UDF(或查找执行此操作的现有第三方 UDF),或作为(Firebird 3+)存储函数。

在这里,这可能会有所帮助。 对不起,西班牙评论,我为我的工作编码。

干杯!

SET TERM ^ ;

create or alter procedure FN_FORMATEA_NUMERO_MILES (
    PVALOR numeric(14,2))
returns (
    NUMERO_FORMATEADO varchar(20))
as
declare variable VDECIMALES varchar(2);
declare variable VENTEROS varchar(20);
declare variable VNUMERO_BASE varchar(20);
declare variable VCONTEO smallint;
declare variable VDIGITOS smallint;
begin
  /* Convierte el numero a caracter */
  vnumero_base = trim(cast(pvalor as varchar(20)));
  /* Separa el segmendo decimal del segmento de enteros */
  vdecimales = substring(vnumero_base from position('.', vnumero_base) + 1 for 2);
  venteros = substring(vnumero_base from 1 for position('.', vnumero_base) - 1);
  /* Inicializa el resultado */
  numero_formateado = '.' || vdecimales;
  /* Integra los enteros en grupos de 3 */
  vconteo = char_length(venteros);
  vdigitos = 0;
  while (vconteo > 0) do
  begin
    numero_formateado = substring(venteros from vconteo for 1) || numero_formateado;
    vconteo = vconteo - 1;
    vdigitos = vdigitos + 1;
    if ((vdigitos = 3) and (vconteo > 0) and (substring(venteros from vconteo for 1) <> '-')) then
    begin
      vdigitos = 0;
      numero_formateado = ',' || numero_formateado;
    end
  end
  /* Regresa el numero formateado */
  suspend;
end^

SET TERM ; ^

/* Existing privileges on this procedure */

GRANT EXECUTE ON PROCEDURE FN_FORMATEA_NUMERO_MILES TO SYSDBA;

暂无
暂无

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

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