Delphi's rounding function round, trunc, ceil, and floor

Source: Internet
Author: User

Cases:
Var
I, J:integer;
Begin
I: = Round (1.5); I equals 2
J: = Round (2.5); J equals 2.
End

The answers used in Delphi with the round function are sometimes not the same as we expected: The four-house six into the five-left double. That is, when the rounding or entering bit is greater than or less than five, it is processed by rounding.

, and when the house or into a bit equal to five o'clock, it is necessary to see what the previous one, according to Chi Jin even, it always returns an even value.
Cases:
i:= Round (11.5)//i equals 12
i:= Round (10.5)//i equals 10

This kind of round is actually according to banker algorithm, statistically generally use this algorithm, than the traditional "rounding" to science.
If you want to use the traditional "rounding" method, you can use the following function:
function Roundclassic (r:real)

2.trunc(Get integer portion of x)
such as:trunc( -123.55) =-123, Floor (123.55) =123

3.Ceil(Gets the smallest integer greater than or equal to X)

such as: Ceil ( -123.55) =-123, Ceil (123.15) =124

4.Floor (gets the largest integer less than or equal to X)

such as: Floor ( -123.55) =-124,floor (123.55) =123

Note: Floor and ceil are functions in the math unit, uses math before use

Delphi's rounding function round, trunc, ceil, and floor

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.