|
Home → WINDEV 25 → MSSQL Arredondamento de números seguindo a norma ABNT NBR 5891 |
MSSQL Arredondamento de números seguindo a norma ABNT NBR 5891 |
Started by Marcilon, Jan., 29 2022 8:22 PM - No answer |
| |
| | | |
|
| |
Posted on January, 29 2022 - 8:22 PM |
Prezados
Segue código enviado pelo Marcilon,
Caso alguém precise, segue a função para *Arredondamento de números seguindo a norma ABNT NBR 5891*
CREATE FUNCTION dbo.fncArredondamento_ABNT ( @Valor NUMERIC(38, 16) ) RETURNS NUMERIC(38, 16) AS BEGIN
DECLARE @Parte_Inteira INT = ROUND(@Valor, 0, 1), @Parte_Decimal NUMERIC(38, 16) = @Valor - ROUND(@Valor, 0, 1), @Nova_Parte_Decimal NUMERIC(38, 16), @SegundoDecimal INT, @DoisPrimeirosDecimais AS NUMERIC(18, 2), @RestanteDosDecimais AS NUMERIC(38, 16)
SELECT @SegundoDecimal = SUBSTRING(CAST(@Parte_Decimal AS VARCHAR(40)), 4, 1), @DoisPrimeirosDecimais = '0.' + SUBSTRING(CAST(@Parte_Decimal AS VARCHAR(40)), 3, 2), @RestanteDosDecimais = '0.' + SUBSTRING(CAST(@Parte_Decimal AS VARCHAR(40)), 5, 16)
SELECT @Nova_Parte_Decimal = (CASE WHEN @RestanteDosDecimais > 0.5 THEN @DoisPrimeirosDecimais + 0.01 WHEN @RestanteDosDecimais < 0.5 THEN @DoisPrimeirosDecimais ELSE @DoisPrimeirosDecimais + IIF(@SegundoDecimal % 2 = 0, 0.00, 0.01) END)
RETURN (@Parte_Inteira + @Nova_Parte_Decimal)
END |
| |
| |
| | | |
|
| | | | |
| | |
|