



É necessário declarar Math na seção uses,
em versões unicode declare System.Math.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | function TBRound(Value: Extended; Decimals: Integer): Extended; var Factor, Fraction: Extended; begin Factor := IntPower(10, Decimals); Value := StrToFloat(FloatToStr(Value * Factor)); Result := Int(Value); Fraction := Frac(Value); if Fraction >= 0.5 then Result := Result + 1 else if Fraction <= -0.5 then Result := Result - 1; Result := Result / Factor; end; |
Exemplos de uso:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | procedure TForm1.Button1Click(Sender: TObject); var f : Double; begin f := 1.36482; // Arredondando para 3 casas decimais. f := SDRound(f, 3); ShowMessage(FloatToStr(f)); // Arredondando para 2 casas decimais. f := SDRound(f, 2); ShowMessage(FloatToStr(f)); end; |
Como alternativa, há outra função que faz o mesmo trabalho:
http://showdelphi.com.br/funcao-para-arredondar-valores/