Pessoal,
A quem se interessar, seque abaixo função para realizar o arredondamento ABNT (Arredondamento utilizado no ECF)
Escrevi na linguagem Harbour, mas, basta apenas trocar os comandos para converter para qualquer outra linguagem (Se precisarem de ajuda na conversão, é só postar aqui, que tentarei ajudar)
FUNCTION Round_ABNT(nValor,nDecimais)
LOCAL nRetorno:=nValor, cDecimais:=SubStr(Str(nValor),At('.',Str(nValor))+1), nSubsequente:=nDecimais+1
if nDecimais<1
RETURN Int(nRetorno)
endif
if Len(cDecimais) <= nDecimais
RETURN nRetorno
endif
if SubStr(cDecimais,nSubsequente,1)>'5' .or. SubStr(cDecimais,nSubsequente,1)<'5' //Se a casa decimal SUBSEQUENTE for DIFERENTE de 5
nRetorno:=Round(nValor,nDecimais) //ARREDONDA
elseif SubStr(cDecimais,nSubsequente,1)=='5' //Se a casa decimal SUBSEQUENTE for IGUAL a 5
if Mod(Val(SubStr(cDecimais,nDecimais,1)),2) <> 0 //Se a casa decimal que será CONSERVADA, for IMPAR
nRetorno:=Round(nValor,nDecimais) //ARREDONDA
else //se a casa decimal que será CONSERVADA, for PAR
if Val(SubStr(cDecimais,nSubsequente+1,1)) > 0 //Se APÓS a casa decimal SUBSEQUENTE, houver ALGUM algarismo MAIOR que ZERO
nRetorno:=Round(nValor,nDecimais) //ARREDONDA
else //Se APÓS a casa decimal SUBSEQUENTE, não houver NENHUM outro algarismo ou TODOS forem iguais a ZERO
nRetorno:=Truncate(nValor,nDecimais) //TRUNCA (Esse é o único momento em que o "arredondamento ABNT" se diferencia do "arredondamento normal")
endif
endif
endif
RETURN nRetorno
FUNCTION Truncate(nValor,nDecimais)
LOCAL nRetorno:=nValor, cDecimais:=SubStr(Str(nValor),At('.',Str(nValor))+1)
if nDecimais<1
RETURN Int(nRetorno)
endif
if Len(cDecimais) <= nDecimais
RETURN nRetorno
endif
nRetorno:=Val( Str(Int(nValor))+'.'+SubStr(cDecimais,1,nDecimais) )
RETURN nRetorno
Nos vários testes que fiz aqui, funcionou muito bem, se encontrarem algum bug, favor, reportar aqui... Obrigado!
Espero ter contribuído,
Abraços,
Reginaldo