4 8 Votes

Delphi/Lazarus: 3 Ways to round a Number to X Decimal Places

Tutorial by Delphian | Last Update on 23/08/2018 | Created on 19/02/2014

Most users of Delphi or even Lazarus are thinking about the function Round when it is about rounding a number. Unfortunately, this function cannot be used to keep some of the decimal places of the original value. Using Round, you can only make 1 from the value 1.2345 and you are not able to round this number to one (1.2), two (1.23) or three decimal places (1.235).

But you can do it in another way. I will show you in this tutorial.

RoundTo

The easiest way is to use the function RoundTo included in Unit Math:

k := 1.2345;
k := RoundTo(k, -2);  // k = 1.23

As a first parameter, we are passing our number we would like to round, as a second parameter the number of decimal places, we would like to keep. We have to use negative values at this point in order to round to decimal places. Positive values can be used to round, for example, to hundreds or thousands: RoundTo(12345, 2) = 12300.

Own Function

It is possible, that older versions of the IDE are not supporting the RoundTo method. In this case, we can just use the following calculation to get to our result:

x := 1.2345;
k := round(x*100)/100;    // 1.23
k := round(x*1000)/1000;  // 1.235

Generalized and swapped out as a function results in the following code:

function RoundEx(const AInput, APlaces): extended;
var
  k: extended;
begin
  k := power(10, APlaces);
  result := round(AInput*k)/k;
end;

We have to pass our value as well as the number of decimal places to this function and we can use it just like RoundTo.

In this Rounding Tutorial for Delphi and Lazarus has introduced an extension of this function. With the function presented there, you are able to round in both directions: you can round to an arbitrary number of decimal places as well as to positions before the decimal point.

Output directly as String

As a third method, I do not want to withhold the possibility to output a floating point number directly and rounded as a string:

x := 1.2345;
s := FormatFloat('0.00', x);           // 1.23
s := FormatFloat('0.000', x);          // 1.235
s := FormatFloat('0.000000', x);       // 1.234500
s := FormatFloat('0.######', x);       // 1.2345
s := FloatToStrF(x, ffFixed, 11, 2);   // 1.23
s := FloatToStrF(x, ffFixed, 11, 3);   // 1.235

For this, we can use the functions FormatFloat or FloatToStrF, for example.

To FormatFloat, we are passing the format as a first parameter and the number as the second parameter. When using a 0 in the format string, this digit is outputted in each case, when using a hash #, this digit is outputted only if necessary.

FloatToStrF is providing many possibilities to format a number. In our case, we only have to know, that we have to pass our value as first parameter and the number of our desired decimal places as the last one.

ReplyPositiveNegativeDateVotes
02 Votes

Your first example 

"k := RoundTo(k, 2); // k = 1.23", 

should be 

"k := RoundTo(k, -2); // k = 1.23".
22/01/2016 at 08:40

ReplyPositive Negative

Delphian

Show Profile | Message
Avatar
35 Votes

Thank you very much for this note.

I have corrected the example and wrote something about the difference between negative and positive values.
23/01/2016 at 14:10

Positive Negative
-13 Votes

There still seems to be a lack of a general purpose function to set the number of digits after the decimal point.

A 2 digit round off should work with all values not just numbers within a small range.

eg. 1.29343e-6 should round to 1.29e-6

It should do nothing to numbers with positive exponents.
11/03/2016 at 21:26

ReplyPositive Negative
-11 Vote

In Lazarus it shows RoundTo as error it doesnt work please help!!!!!!!

I need to round a real number to 2 decimals.
07/03/2018 at 08:28

ReplyPositive Negative

Delphian

Show Profile | Message
Avatar
02 Votes

Please tell us what error message you got.

Perhaps, you have forgotten to include the unit "math" to your uses clause.
07/03/2018 at 13:24

Positive Negative
02 Votes

In this example RoundTo(123.375,-2) should round to 123.38 but it rounds to 123.38!!

Whats wrong?
06/03/2019 at 18:32

ReplyPositive Negative
02 Votes

In this example RoundTo(124.375,-2) should round to 124.38 but it rounds to 124,37!!!

Whats wrong?
06/03/2019 at 18:34

ReplyPositive Negative
Reply

About the Author

Avatar AuthorThe author has not added a short description to his profile yet.
Show Profile | Message

 

Related Topics

PHP: Rounding Numbers
Tutorial | 0 Comments

Important Note

Please note: The contributions published on askingbox.com are contributions of users and should not substitute professional advice. They are not verified by independents and do not necessarily reflect the opinion of askingbox.com. Learn more.

Participate

Ask your own question or write your own articles on askingbox.com. How to do.