Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Trunc Method in Delphi

Tags:

c#

delphi

I am porting some code to .Net and I realized that the Trunc method is not properly working in Delphi. It doesn't account whether you are truncating a negative or positive value. It treats the argument as absolute.

Delphi

Trunc(-163.78999) returns -163

.Net Implementation

System.Math.Floor(-163.88888888888889) returns -164.

I can mimic the implementation but I am wondering if anyone has seen this before.

Thanks,

like image 504
Sam Avatar asked Dec 19 '22 07:12

Sam


1 Answers

Delphi's Trunc function performs as designed. The documentation says (emphasis mine):

Truncates a real number to an integer.

In Delphi code, the Trunc function truncates a real-type value to an integer-type value. X is a real-type expression. Trunc returns an Int64 value that is the value of X rounded toward zero.

The .net equivalent to Delphi Trunc is Math.Truncate.

The Delphi equivalent to .net System.Math.Floor is Floor.

like image 167
David Heffernan Avatar answered Dec 24 '22 02:12

David Heffernan