0 2 Votes

Delphi/Lazarus: Round Decimal Numbers up, down and normally

Tip by Delphian | 27/06/2013 at 14:49

In this tip, I would like to show you how you can round decimal numbers such as extended, float or real numbers using Delphi or Lazarus. You have the possibility to round numbers up, down or according to Bankers' Rules.

Ovierview: Lazarus as well as Delphi are providing the following functions you can use for rounding:

  • Round: normal, convergent rounding (Banker's Rule)
  • Trunc: crops decimal places (=rounding down)
  • Ceil: rounding up (contained in in Math)
  • Floor: rounding down (contained in Math)
  • Int: makes an integer value from a decimal number (=rounding down)
  • Frac: crops all before the decimal places/replaces the integer part of the number with 0

Example: In this example, I show you which results you can expect when using this functions.

Call         Result       Call         Result 
round(7.2)   7            round(7.8)   8
trunc(7.2)   7            trunc(7.8)   7
ceil(7.2)    8            ceil(7.8)    8
floor(7.2)   7            floor(7.8)   7
int(7.2)     7            int(7.8)     7 
frac(7.2)    0.2          frac(7.8)    0.8

Hint: For the functions Ceil() and Floor(), the unit "Math" has to be added to the uses-clause. Round(), Trunc(), Int() and Frac() are parts of the unit "System" which is usually already included in the uses section.

ReplyPositiveNegative

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.