Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Semrush - keyword research tool

Turn ChatGPT, Claude, Gemini, And CoPilot Into Your Personal Assistant, Business Coach, Content Creator, And More

AFFILIATE MARKETING Your all-in-one performance engine Manage affiliates, creators, and customer referrals in one unified platform—turning every partnership into measurable growth
Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

42,596 questions

55,330 answers

573 users

How to convert a decimal to a long in Pascal

1 Answer

0 votes
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

}

 



answered 1 day ago by avibootz
...