program DecimalToLongProgram;
{$mode objfpc}{$H+}
{
============================================================
Convert a decimal-like value to a LongInt in Free Pascal.
This program demonstrates:
• Conversion using Round(), which rounds to the nearest integer.
• Conversion using Trunc(), which truncates toward zero.
• A helper procedure that prints both results for comparison.
Notes:
• Free Pascal does not have a built-in decimal type.
• Real/Double/Extended are used for decimal values.
• Round() follows standard rounding rules.
• Trunc() removes the fractional part.
============================================================
}
{ Converts a floating decimal value to LongInt using rounding }
function ConvertDecimalToLong(value: Double): LongInt;
begin
Result := Round(value);
end;
{ Converts a floating decimal value to LongInt using truncation }
function CastDecimalToLong(value: Double): LongInt;
begin
Result := Trunc(value);
end;
{ Prints both conversion styles for comparison }
procedure ShowConversions(value: Double);
var
rounded, truncated: LongInt;
begin
Writeln('Input decimal: ', value:0:4);
rounded := ConvertDecimalToLong(value);
truncated := CastDecimalToLong(value);
Writeln('Rounded (Round): ', rounded);
Writeln('Truncated (Trunc): ', truncated);
Writeln;
end;
begin
{ Example values to demonstrate behavior }
ShowConversions(12.7);
ShowConversions(12.3);
ShowConversions(-5.8);
ShowConversions(42.0); { already an integer }
end.
{
run:
Input decimal: 12.7000
Rounded (Round): 13
Truncated (Trunc): 12
Input decimal: 12.3000
Rounded (Round): 12
Truncated (Trunc): 12
Input decimal: -5.8000
Rounded (Round): -6
Truncated (Trunc): -5
Input decimal: 42.0000
Rounded (Round): 42
Truncated (Trunc): 42
}