본문 바로가기

코딩 이야기/델파이 코딩

델파이 숫자 함수 종류 및 예시

델파이 숫자 함수 종류 및 예시

델파이에서 숫자를 다루는 함수들은 주로 SysUtils 유닛과 Math 유닛에 포함되어 있습니다.

사용해야 할 PAS 유닛

uses SysUtils, Math;

주요 숫자 함수들은 기능별로 분류하여 아래에 설명되어 있습니다.

1. 기본 산술 연산 및 형 변환

기본 산술 연산자 (+, -, *, /, div, mod)

기능: 더하기, 빼기, 곱하기, 나누기, 정수 나누기, 나머지 연산입니다.

예시:

var
  a, b, resultInt: Integer;
  resultReal: Double;
begin
  a := 10;
  b := 3;

  resultInt := a + b;     // 13
  resultInt := a - b;     // 7
  resultInt := a * b;     // 30
  resultReal := a / b;    // 3.333... (실수)
  resultInt := a div b;   // 3 (정수 나누기)
  resultInt := a mod b;   // 1 (나머지)

  ShowMessage(Format('더하기: %d', [a + b]));
  ShowMessage(Format('나누기: %.2f', [a / b]));
  ShowMessage(Format('정수 나누기: %d', [a div b]));
  ShowMessage(Format('나머지: %d', [a mod b]));
end;

실수 → 정수 변환 함수 (Int, Trunc, Round)

기능: 실수를 정수로 변환합니다. Int는 실수형 결과, TruncRound는 정수형 결과를 반환합니다.

예시:

var
  x: Double;
  iResult, tResult, rResult: Integer;
  intResult: Double;
begin
  x := 3.7;
  intResult := Int(x);    // 3.0 (Double)
  tResult := Trunc(x);    // 3 (Integer)
  rResult := Round(x);    // 4 (Integer)

  ShowMessage(Format('Int(3.7): %.1f', [intResult]));
  ShowMessage(Format('Trunc(3.7): %d', [tResult]));
  ShowMessage(Format('Round(3.7): %d', [rResult]));

  x := 3.3;
  rResult := Round(x);    // 3 (Integer)
  ShowMessage(Format('Round(3.3): %d', [rResult]));

  x := 3.5;
  rResult := Round(x);    // 4 (Integer)
  ShowMessage(Format('Round(3.5): %d', [rResult]));
end;

2. 문자열 ↔ 숫자 변환

IntToStr 함수

기능: 정수 값을 문자열로 변환합니다. (SysUtils)

예시:

var
  MyInteger: Integer;
  MyString: string;
begin
  MyInteger := 123;
  MyString := IntToStr(MyInteger);
  ShowMessage('정수 -> 문자열: ' + MyString); // 결과 예시: 정수 -> 문자열: 123
end;

StrToInt 함수

기능: 문자열을 정수 값으로 변환합니다. 변환할 수 없으면 예외가 발생합니다. (SysUtils)

예시:

var
  MyString: string;
  MyInteger: Integer;
begin
  MyString := '456';
  try
    MyInteger := StrToInt(MyString);
    ShowMessage(Format('문자열 -> 정수: %d', [MyInteger])); // 결과 예시: 문자열 -> 정수: 456
  except
    on EConvertError do
      ShowMessage('잘못된 정수 형식 문자열입니다.');
  end;
end;

FloatToStr 함수

기능: 부동 소수점 값을 문자열로 변환합니다. 시스템의 부동 소수점 형식 설정을 따릅니다. (SysUtils)

예시:

var
  MyFloat: Double;
  MyString: string;
begin
  MyFloat := 123.456;
  MyString := FloatToStr(MyFloat);
  ShowMessage('실수 -> 문자열: ' + MyString); // 결과 예시: 실수 -> 문자열: 123.456 (시스템 설정에 따라 다를 수 있음)
end;

StrToFloat 함수

기능: 문자열을 부동 소수점 값으로 변환합니다. 변환할 수 없으면 예외가 발생합니다. (SysUtils)

예시:

var
  MyString: string;
  MyFloat: Double;
begin
  MyString := '78.9';
  try
    MyFloat := StrToFloat(MyString);
    ShowMessage(Format('문자열 -> 실수: %.1f', [MyFloat])); // 결과 예시: 문자열 -> 실수: 78.9
  except
    on EConvertError do
      ShowMessage('잘못된 실수 형식 문자열입니다.');
  end;
end;

FormatFloat 함수

기능: 부동 소수점 값을 지정된 포맷 지정 문자열에 따라 포맷된 문자열로 변환합니다. (SysUtils)

포맷 지정자: '0', '#', '.', ',', 'E'/'e'

예시:

var
  Value: Double;
  FormattedString: string;
begin
  Value := 12345.678;

  FormattedString := FormatFloat('0.00', Value);    // 결과 예시: 12345.68 (반올림)
  ShowMessage('포맷 (0.00): ' + FormattedString);

  FormattedString := FormatFloat('#,##0.00', Value); // 결과 예시: 12,345.68
  ShowMessage('포맷 (#,##0.00): ' + FormattedString);

  Value := 0.12345;
  FormattedString := FormatFloat('0.00E+00', Value); // 결과 예시: 1.23E-01
  ShowMessage('포맷 (과학적): ' + FormattedString);
end;
FloatToStrF 함수도 부동 소수점 포맷에 사용되며, FormatFloat과 유사하지만 포맷 지정 방식이 다릅니다.

3. 수학 함수

Abs 함수

기능: 숫자의 절댓값을 반환합니다. (SysUtils)

예시:

var
  x, result: Integer;
begin
  x := -10;
  result := Abs(x); // 10
  ShowMessage(Format('Abs(%d): %d', [x, result]));
end;

Sqrt 함수

기능: 숫자의 제곱근을 반환합니다. (Math)

예시:

var
  x, result: Double;
begin
  x := 16.0;
  result := Sqrt(x); // 4.0
  ShowMessage(Format('Sqrt(%.1f): %.1f', [x, result]));
end;

Power 함수

기능: 밑수를 지수만큼 거듭제곱한 값을 반환합니다. Power(Base, Exponent) (Math)

예시:

var
  base, exponent, result: Double;
begin
  base := 2.0;
  exponent := 3.0;
  result := Power(base, exponent); // 2의 3제곱 = 8.0
  ShowMessage(Format('Power(%.1f, %.1f): %.1f', [base, exponent, result]));
end;

Sin, Cos, Tan 함수

기능: 각도의 사인, 코사인, 탄젠트 값을 라디안 단위로 계산합니다. (Math)

예시:

var
  angleRad, sinVal, cosVal: Double;
begin
  angleRad := Pi / 2; // 90도 (라디안)
  sinVal := Sin(angleRad); // 약 1.0
  cosVal := Cos(angleRad); // 약 0.0
  ShowMessage(Format('Sin(Pi/2): %.1f', [sinVal]));
  ShowMessage(Format('Cos(Pi/2): %.1f', [cosVal]));
end;
Math 유닛에는 로그 (Log10, Ln), 거듭제곱근 (Power), 삼각 함수 외에도 다양한 수학 함수들이 포함되어 있습니다.

Min, Max 함수

기능: 두 값 중 최솟값 또는 최댓값을 반환합니다. 오버로드되어 있어 정수, 실수 등 다양한 타입에 사용할 수 있습니다. (SysUtils)

예시:

var
  a, b, minVal, maxVal: Integer;
begin
  a := 10;
  b := 20;
  minVal := Min(a, b); // 10
  maxVal := Max(a, b); // 20
  ShowMessage(Format('Min(%d, %d): %d', [a, b, minVal]));
  ShowMessage(Format('Max(%d, %d): %d', [a, b, maxVal]));
end;