Topic: Rounding to the Nearest Whole Number

Hello,

I need round up to number without fractions, I create this script but I believe there is much butter way to rounding up numbers.

Anyone will be welcome to optimize the script (the Project in Attachment)

Var
Correction : Real;
procedure Form1_Button1_OnClick (Sender: TObject; var Cancel: boolean);
begin
Correction := Form1.Edit1.Value - Trunc(Form1.Edit1.Value);       // Find fraction value
If (Correction < 0.5) And (Correction <> 0) Then Correction := 0.5 Else Correction := 0;
Form1.Edit2.Value := Round(Form1.Edit1.Value + Correction);
end;
Post's attachments

Attachment icon Round-Up.zip 3.79 kb, 38 downloads since 2025-12-13 

Life is like a school;
One can learn and graduate or stay behind.

Re: Rounding to the Nearest Whole Number

Hi Fmr,
You could try it like this (see attached) but there are probably other ways too.
The 'round' command by default rounds up or down depending on the value used so if it rounds down, simply add 1 to the result.
Regards,
Derek.

Post's attachments

Attachment icon rounding.zip 435.14 kb, 46 downloads since 2025-12-13 

Re: Rounding to the Nearest Whole Number

Hi derek,

Your script work like magic, simple and accurate

Life is like a school;
One can learn and graduate or stay behind.

Re: Rounding to the Nearest Whole Number

Hi


Function DecimalRoundExt(Value, NDFD, Ctrl): Extended;

   (Value: extended;     {Input value to be rounded.}
    NDFD: integer;       {Number decimal fraction digits to figure in result.}
    Ctrl: tDecimalRoundingCtrl = drHalfEven)  {Optional rounding rule}


Type tDecimalRoundingCtrl =    {The defined rounding methods}
   (drNone,    {No rounding.}
    drHalfEven,{Round to nearest or to even whole number. (a.k.a Bankers) }
    drHalfPos, {Round to nearest or toward positive.}
    drHalfNeg, {Round to nearest or toward negative.}
    drHalfDown,{Round to nearest or toward zero.}
    drHalfUp,  {Round to nearest or away from zero.}
    drRndNeg,  {Round toward negative.                    (a.k.a. Floor) }
    drRndPos,  {Round toward positive.                    (a.k.a. Ceil ) }
    drRndDown, {Round toward zero.                        (a.k.a. Trunc) }
    drRndUp);  {Round away from zero.}