⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 jvconverter.pas

📁 East make Tray Icon in delphi
💻 PAS
📖 第 1 页 / 共 2 页
字号:
constructor TJvConverter.Create(AOwner: TComponent);
begin
  inherited Create(AOwner);
  FData := '';
  FDataType := dtString;
  FPrecision := 15;
  FDigits := 2;
  FDateTimeFormat := TJvDateTimeFormat.Create;
  FTextValues[False] := RsFalse;
  FTextValues[True] := RsTrue;
  FRaiseOnError := False;
end;

destructor TJvConverter.Destroy;
begin
  FDataType := dtString;
  //if (FData <> nil) and (FData^ <> '') then Dispose(FData);
  FDateTimeFormat.Free;
  inherited Destroy;
end;

procedure TJvConverter.Clear;
begin
  //if (FData <> nil) and (FData^ <> '') then Dispose(FData);
  FData := '';
  Change;
end;

procedure TJvConverter.Change;
begin
  if Assigned(FOnChange) then
    FOnChange(Self);
end;

function TJvConverter.GetString: string;
begin
  Result := FData;
end;

procedure TJvConverter.SetString(const Value: string);
begin
  FData := Value;
end;

function TJvConverter.GetDateTimeFormat: TJvDateTimeFormat;
begin
  Result := FDateTimeFormat;
end;

procedure TJvConverter.SetDateTimeFormat(Value: TJvDateTimeFormat);
begin
  FDateTimeFormat.Assign(Value);
end;

function TJvConverter.GetBoolValues(Index: Integer): string;
begin
  Result := FTextValues[Boolean(Index)];
end;

procedure TJvConverter.SetBoolValues(Index: Integer; const Value: string);
begin
  FTextValues[Index <> 0] := Value;
end;

function TJvConverter.BoolToStr(Value: Boolean): string;
begin
  Result := GetBoolValues(Ord(Value));
end;

function TJvConverter.FloatToString(Value: Double): string;
begin
  Result := FloatToStrF(Value, FloatFormat, Precision, Digits);
end;

function TJvConverter.DateTimeToString(Value: TDateTime): string;
begin
  case FDataType of
    dtDate:
      Result := FormatDateTime(DateTimeFormat.DateMask, Value);
    dtTime:
      Result := FormatDateTime(DateTimeFormat.TimeMask, Value);
  else
    Result := FormatDateTime(DateTimeFormat.Mask, Value);
  end;
end;

procedure TJvConverter.SetDataType(Value: TDataType);
begin
  if Value <> FDataType then
  begin
    FDataType := Value;
    try
      CheckDataType;
      Change;
    except
      Clear;
      if RaiseOnError then
        raise;
    end;
  end;
end;

function TJvConverter.IsValidChar(Ch: Char): Boolean;
begin
  case FDataType of
    dtString:
      Result := True;
    dtInteger:
      Result := Ch in DigitSymbols + SignSymbols;
    dtFloat:
      Result := Ch in DigitSymbols + SignSymbols + [DecimalSeparator, 'E', 'e'];
    dtDateTime, dtDate, dtTime:
      Result := True;
    dtBoolean:
      Result := True;
  else
    Result := False;
  end;
end;

procedure TJvConverter.CheckDataType;
begin
  case FDataType of
    dtInteger, dtFloat:
      StrToFloat(GetString);
    dtDateTime, dtDate, dtTime:
      GetDateTime;
  end;
end;

function TJvConverter.GetAsBoolean: Boolean;
var
  S: string;
begin
  S := GetString;
  Result := (Length(S) > 0) and ((S[1] in ['T', 't', 'Y', 'y']) or
    (S = FTextValues[True]));
end;

function TJvConverter.GetDateTime: TDateTime;
var
  S: string;
  I: Integer;
  DateS, TimeS: set of Char;
begin
  S := GetString;
  DateS := ['/', '.'] + [DateTimeFormat.DateSeparator] -
    [DateTimeFormat.TimeSeparator];
  TimeS := [':', '-'] - [DateTimeFormat.DateSeparator] +
    [DateTimeFormat.TimeSeparator];
  for I := 1 to Length(S) do
  begin
    if S[I] in DateS then
      S[I] := DateSeparator
    else
    if S[I] in TimeS then
      S[I] := TimeSeparator;
  end;
  Result := StrToDateTime(S);
end;

function TJvConverter.GetAsDateTime: TDateTime;
begin
  try
    Result := GetDateTime;
  except
    Result := NullDate;
  end;
end;

function TJvConverter.GetAsDate: TDateTime;
var
  Year, Month, Day: Word;
begin
  try
    Result := GetAsDateTime;
    DecodeDate(Result, Year, Month, Day);
    Result := EncodeDate(Year, Month, Day);
  except
    Result := NullDate;
  end;
end;

function TJvConverter.GetAsTime: TDateTime;
var
  Hour, Min, Sec, MSec: Word;
begin
  try
    Result := GetAsDateTime;
    DecodeTime(Result, Hour, Min, Sec, MSec);
    Result := EncodeTime(Hour, Min, Sec, MSec);
  except
    Result := NullDate;
  end;
end;

function TJvConverter.GetAsFloat: Double;
begin
  try
    case FDataType of
      dtDateTime:
        Result := GetAsDateTime;
      dtDate:
        Result := GetAsDate;
      dtTime:
        Result := GetAsTime;
    else
      Result := StrToFloat(GetString);
    end;
  except
    Result := 0.0;
  end;
end;

function TJvConverter.GetAsInteger: Longint;
begin
  Result := Round(GetAsFloat);
end;

function TJvConverter.GetAsString: string;
begin
  case FDataType of
    dtString:
      Result := GetString;
    dtInteger:
      Result := IntToStr(GetAsInteger);
    dtFloat:
      Result := FloatToString(GetAsFloat);
    dtDateTime:
      Result := DateTimeToString(GetAsDateTime);
    dtDate:
      Result := DateTimeToString(GetAsDate);
    dtTime:
      Result := DateTimeToString(GetAsTime);
    dtBoolean:
      Result := BoolToStr(GetAsBoolean);
  end;
end;

procedure TJvConverter.SetAsBoolean(Value: Boolean);
begin
  SetAsString(BoolToStr(Value));
end;

procedure TJvConverter.SetAsDateTime(Value: TDateTime);
begin
  SetAsString(DateTimeToStr(Value));
end;

procedure TJvConverter.SetAsDate(Value: TDateTime);
begin
  SetAsDateTime(Value);
end;

procedure TJvConverter.SetAsTime(Value: TDateTime);
begin
  SetAsDateTime(Value);
end;

procedure TJvConverter.SetAsFloat(Value: Double);
begin
  if FDataType in [dtDateTime, dtDate, dtTime] then
    SetAsDateTime(Value)
  else
    SetAsString(FloatToStr(Value));
end;

procedure TJvConverter.SetAsInteger(Value: Longint);
begin
  if FDataType = dtInteger then
    SetAsString(IntToStr(Value))
  else
    SetAsFloat(Value);
end;

procedure TJvConverter.SetAsString(const Value: string);
var
  S: string;
begin
  S := GetString;
  SetString(Value);
  try
    CheckDataType;
    Change;
  except
    SetString(S);
    if RaiseOnError then
      raise;
  end;
end;

{$IFDEF UNITVERSIONING}
initialization
  RegisterUnitVersion(HInstance, UnitVersioning);

finalization
  UnregisterUnitVersion(HInstance);
{$ENDIF UNITVERSIONING}

end.

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -